Skip to content
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

TypeScript: parse returns type unknown #250

Closed
jedkass opened this issue Oct 20, 2023 · 3 comments
Closed

TypeScript: parse returns type unknown #250

jedkass opened this issue Oct 20, 2023 · 3 comments

Comments

@jedkass
Copy link

jedkass commented Oct 20, 2023

parse clearly returns type JavaScriptValue, which I can see in the types declaration file is:

export type JavaScriptValue = {
    [key: string]: JavaScriptValue;
} | JavaScriptValue[] | JavaScriptPrimitive;

Yet when I call parse, TypeScript is telling me the return type is unknown. If I cast the return value to JavaScriptValue, TypeScript tells me:

(alias) type JavaScriptValue = unknown

Do you know what might be happening here?

@josdejong
Copy link
Owner

Thanks for your input.

In lossless-json I tried to capture the return type with JavaScriptValue, but it doesn't really work out nicely. The build-in JSON.parse from JavaScript is defined to return any. This makes sense, since a reviver may return anything and you can't really guarantee what will be returned. So, I think best will be to remove JavaScriptValue.

I'm not sure though why TypeScript thinks that parse returns unknown. How can I reproduce your issue?

@josdejong
Copy link
Owner

I created a PR to improve the types, see #251. Does that make sense @jedkass ?

@josdejong
Copy link
Owner

I've now published v3.0.0 that has replaced the JavaScriptValue type with unknown.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants