You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
ts2322 incorrectly triggered when importing jsonΒ #58734
Type '{ field: number[]; }[]' is not assignable to type 'MyType[]'.
Type '{ field: number[]; }' is not assignable to type 'MyType'.
Types of property 'field' are incompatible.
Type 'number[]' is not assignable to type '[number, number]'.
Target requires 2 element(s) but source may have fewer.
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:
constt: MyType[]=[{"field": [1,200]}];
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 to number[] and not to [number, number].
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...
In my case TS incorrectly flags this as an error, even though it shouldn't.
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 { ... } as const to prevent widening, while there's no equivalent syntax for imports. That's what #32063 is about.
Activity
fatcerberus commentedon Jun 1, 2024
This is more or less equivalent to
which has the same error (for the same reason).
MartinJohns commentedon Jun 1, 2024
You would need something like #32063.
jcalz commentedon Jun 1, 2024
Yeah, I'd consider this a duplicate of #32063
MarByteBeep commentedon Jun 1, 2024
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...
MartinJohns commentedon Jun 1, 2024
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.
MarByteBeep commentedon Jun 1, 2024
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.
fatcerberus commentedon Jun 1, 2024
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
{ ... } as constto prevent widening, while there's no equivalent syntax for imports. That's what #32063 is about.MarByteBeep commentedon Jun 1, 2024
Got it. Thanks for elaborating that!
typescript-bot commentedon Jun 16, 2024
This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.