-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Stricter types for JSON
#51003
Comments
This doesn’t seem very useful; it only works if you’re passing string literals to |
You are mistaken. |
That would exclude a ton of perfectly JSON-able objects. For example, tree structures. |
For example? |
@fatcerberus can you provide an example of something which this PR does not correctly parse or stringify? Here you will find examples of a tree structure being parsed and stringified correctly: |
My point was, your types are excluding |
const emptyString = ''
JSON.parse(emptyString) // no error |
If you know your string statically at compile time, why do you need to parse it anyway? |
Cases in point: const circularReference: { myself?: any } = {};
circularReference.myself = { foo: 812 };
JSON_Override.stringify(circularReference); // error?! const str = '{ "foo": "validJSON" }'; // pretend this was read from a file...
JSON_Override.parse(str); // error. this is not useful. The types only exist at compile time. It seems like you might not fully understand how the type system works? |
Why are you saying that If you have an example of where these proposed stricter types are actually incorrect, feel free to post a link to a reproducible TS Playground. Also, I need to point out that saying things like "This doesn’t seem very useful" and "It seems like you might not fully understand how the type system works" is against this repository's Code of Conduct. Frankly, it's embarrassing. I urge you to rethink the way you interact with other human beings. You are correct about the circular dependency point though, so I will amend the PR accordingly. |
let str = '{ "foo": "validJSON" }';
JSON_Override.parse(str); // error Like I said, these extra checks only work if you're passing literal-typed strings to |
you're just pasting code and adding here is a TS Playground which shows that If you can reproduce erroneous behaviour in TS Playground, feel free to send it. |
It absolutely is an error. You changed the |
@Josh-Cena A case might be using |
@brunocrosier According to this, all of the values you provided should be parsable, including the empty string |
For the record, “this isn’t a useful change” and “this seems like a misunderstanding of how TypeScript works” are mere observations and don’t constitute CoC violations. I think it’s fair to criticize a feature request on those grounds, no? Anyway: types are not values, and they exist only at compile time. The proposed checks for |
Your implementation of "JSON_Override" wrongly prevents leading or tailing whitespace characters. In general, as mentioned, this only works for string literals. A very narrow use case, but a whole buttload of added complexity, resulting in compilation slowdown and breaking changes (added global types). Especially such a generic name like |
I'm not sure why |
It has such a generic name, odds are such a name is already in use, resulting in a breaking change. The introduction of such a utility type also means the team can't update it in the future (if a new feature would be better suited for such a type), without causing yet another breaking change (see
|
You are right. At the moment, the types in this PR at the moment are too strict - particularly because of I still believe there is a lot of benefit in disallowing the parsing of an empty string, or the stringification of a What about something like this? It disallows values we know cannot be parsed / stringified (as opposed to the previous solution which would try to codify the allowed ones as a type and resolve to |
It doesn't address the problems with the utility types I mentioned. Also, you ignore the possibility of providing a replacer. This is perfectly valid: |
That's a good point re: the replacer. How about this?
|
I'm no member of the TypeScript team, so I have no say. But I don't see the point in vastly overcomplicating these types for a negligible use case. This would only work for inferred string literal types, and I can't think of a reason why you would pass a string literal to Honestly, this would be much better suited as an ESLint rule. |
Yeah, you're right actually. My bad |
lib Update Request
Configuration Check
My compilation target is
ES2015
and my lib isthe default
.Missing / Incorrect Definition
JSON.parse()
andJSON.stringify()
The definitions for both of these functions currently accept a
string
, but this is not strict enough. Not all strings can be parsed or stringified. Many of these can be caught at a type level, meaning they could and should be added to the type definitions.Sample Code
TS Playground: https://www.typescriptlang.org/play?target=99#code/PTAEAEBcGcFoFMAeAHeBjSCBOWD2WAoAKQGUB5AOQDpkBDLaeACgHIWBKAgkCGBFdJng58xctToNmAIhYAzXLhbT2Abi48ocJKgzY8hUpRr1GTaQG0AjABpQAJjsBmOwBY7AXRXruYLf10hEUNxEylWAH4ODT8+HUF9USMJUxkAI3oVGN5tAT1hAzFjaEgsAEsAOwBzMrkATyYAbwJQVtAAa3g6gC4HCoIAX04CNFwKktA0Mqw0AFcAG3oAJXg5YXgKtHhextAAWzrGebkI3toKutAB0ABeUEaB9SmZheXV9c34KgOjuVvJ6ZzRZYFZrLAbLY+TRxPJBQrJKglcrVWoNZ5At5giHwThAA
Documentation Link
JSON
JSON.parse
JSON.stringify
The text was updated successfully, but these errors were encountered: