-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
ts2322 incorrectly triggered when importing json #58734
Comments
This is more or less equivalent to const test = [
{
"field": [1, 200]
}
];
const t: MyType[] = test; which has the same error (for the same reason). |
You would need something like #32063. |
Yeah, I'd consider this a duplicate of #32063 |
Even though I see the similarities, I think that proposal goes a bit further (and will probably fix this as well). In my case TS incorrectly flags this as an error, even though it shouldn't. Hopefully this is easier to fix than #32063, as that one has been open for 5 years already... |
It doesn't flag it incorrectly as an error. You're just wrongly assuming the imported JSON has a narrowed type (the tuple), when it's just the widened type (an array). You expect a feature that's not supported. |
Look, I'm just reporting in my opinion inconsistent behavior. When doing seemingly identical operations (direct assignment vs assignment through a JSON import), I get different results and spent a long time figuring out why. I understand now why that happens, but from a design perspective this is not very intuitive. Just my two cents. |
@MarByteBeep The inconsistent behavior is the same as what's illustrated in my post above. The direct assignment works because there's a contextual type that TS can use as a hint to avoid widening too far. In the case of the indirect assignment, that doesn't happen and the object literal widens, leading to an assignability error down the line. That widening is by design, and the JSON import isn't special in that regard. The only difference is that, for an object literal written inline, you can write |
Got it. Thanks for elaborating that! |
This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
π Search Terms
ts2322, json, array
π Version & Regression Information
I did test this on v5.5.0-dev.20240601
β― Playground Link
I wasn't able to add an external JSON to the Playground
π» Code
Create
test.json
with following contentNow run the following code
π Actual behavior
Observe that ts2322 is triggered:
For some reason TS assumes there may be fewer elements, even though the JSON clearly has a single element with an array containing exactly two numbers. When copying the content directly into the ts file, like so:
no such error is raised.
π Expected behavior
I expect the result of the imported JSON to be identical to when directly assigning the values in the typescript file. The contents of the JSON array are known at compile time, so there is no need to raise ts2322.
Additional information about the issue
I suspect
ts
converts the type of the imported field tonumber[]
and not to[number, number]
.The text was updated successfully, but these errors were encountered: