Typescript: Add ability to `JSON.parse` function to specify the return type

Created on 5 Mar 2019  Â·  10Comments  Â·  Source: microsoft/TypeScript

Search Terms

  • JSON
  • JSON.parse
  • JSON.parse return type

Suggestion

Add the ability to JSON.parse function to specify the return type, I already opened a PR #30219, implementing this feature

Use Cases

let parsedObj = JSON.parse<TypeObj>("{ prop: 'val' }"); // parsedObj is TypeObj

is way better than

let parsedObj = JSON.parse("{ prop: 'val' }"); // parsedObj is any

Examples

that said in the use cases ¯_(ツ)_/¯

Checklist

My suggestion meets these guidelines:

  • [x] This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • [x] This wouldn't change the runtime behavior of existing JavaScript code
  • [x] This could be implemented without emitting different JS based on the types of the expressions
  • [x] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • [x] This feature would agree with the rest of TypeScript's Design Goals.
Working as Intended

Most helpful comment

JSON.parse<TypeObj>("{ prop: 'val' }");

There's already a place to put that:

<TypeObj>JSON.parse("{ prop: 'val' }");

All 10 comments

While JSON.parse returns any, you can simply do
let parsedObj: Interface = JSON.parse("{ prop: 'val' }"); // parsedObj is any

So there will be no difference between adding generic type to JSON.parse and mine "solution"

@hejkerooo here's a real-world example

export const getCurrentUserDataAsync = () => SecureStore.getItemAsync(SESSION_KEY).then(data => data ? JSON.parse<LoginData>(data) : undefined);

How can your solution solve it?

type ReturnType = LoginData | undefined;
export const getCurrentUserDataAsync = (): ReturnType => SecureStore.getItemAsync(SESSION_KEY).then(data => data ? JSON.parse(data) : undefined);

But you shouldn't return undefined, consider throwing an error or try nullable pattern
Nullable pattern

edit
You should not declare return type basic on what is function returning, but function declaration itself

@hejkerooo so, for a service file that contains about ten methods like this one I'm going to define ten types separately!!!

I think this is what a type assertion is for.

JSON.parse(data) as LoginData
// or: <LoginData>JSON.parse(data)

@hejkerooo so, for a service file that contains about ten methods like this one I'm going to define ten types separately!!!

I think, this is a point of strongly typed languages

Already discussed and rejected as Working as intended: #26993 and #26994.

@j-oliveras but #26994 and #28416 discussing solutions that don't make sense, can you please refer to #30219, see the changes and tell me what you think?!
anyways it's up to the community to decide whether this update is useful or not ¯_(ツ)_/¯

JSON.parse<TypeObj>("{ prop: 'val' }");

There's already a place to put that:

<TypeObj>JSON.parse("{ prop: 'val' }");

@RyanCavanaugh you're right 😅

Was this page helpful?
0 / 5 - 0 ratings