-
Notifications
You must be signed in to change notification settings - Fork 12.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
More specific JSON parse reviver argument in lib.d.ts #6955
Comments
The problem here is similar to for..in issues, see the fix in TS 1.8 in #6379. The value is a string, but it could be a numeric string in cases of array. it is not a number value (i.e. can not call toExponent on it) but it can still be used to index into an array (since at the end all index operations first do a toString in the key anyways). TypeScript, tries to strongly type index operations using index signatures. there are two, string and numeric. and if you index into an object using a type that it does not have an index signature for, you get an any. ideally, this so: var list = [0, 1];
JSON.parse("[3, 4]", (k:string, v) => {
var e = list[k]; // e is any, as list has no string indexer,
// also an error with --noImplicitAny
})
JSON.parse("[3, 4]", (k:number, v) => {
var e = list[k]; // e number, as expected
k.toExponential() // runtime error, k is not a number
}) in short, this was intentional. but maybe we should reconsider. |
I understand your reasoning, but how is this different from the stringify replacer which is kind of a sibling to the reviver?
I see this as a callback with clearly defined arguments, the first being a string. Nothing prevents you from passing in a function which accepts (any, any), but it would prevent you from passing in (number, any) which is currently allowed but wrong to my understanding:
|
PR welcomed on this one (be sure to read CONTRIBUTING.md on how to submit a lib.d.ts PR) |
Hey, I've been wanting to get more involved with the TypeScript code base but simply lack the knowledge about the current implementation (code base is huge and intimidating, I've only written toy interpreters). This issue looks simple enough, I think. Just seems to be updating I looked at DefinitelyTyped/DefinitelyTyped#15744 and their export type Reviver = (this: {}, key: string, value: any) => any; I think the JSON.parse(`{ "x":42 }`, function (k, v) {
//Element implicitly has an 'any' type because type '{}' has no index signature.
const x = this[k];
}); If we use It also looks like
I ran |
I also just noticed there are trailing spaces in 4 lines of comments in I assume no one wants these removed. I made the changes in the two places and Assuming this is all good, do I just need to run Also, I tried signing the CLA (Contributor License Agreement) at https://cla.opensource.microsoft.com/ but I think it's broken. Unless signing in was the same as signing the CLA... |
…y-replacer-better-declaration JSON.parse(), JSON.stringify() more specific declarations for #6955
Currently, the JSON interface in lib.d.ts declares the following:
However, my understanding is that the spec guaratees a string for the key (see http://www.ecma-international.org/ecma-262/5.1/#sec-15.12.2 paragraph "The abstract operation Walk ... String name ..."), in consequence it could be more specific:
The text was updated successfully, but these errors were encountered: