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
importdatafrom"./data.json";interfaceFoo{str: string;}consta: Foo[]=data.foo;// no errorconstb: Foo[]=data.foo;// compilation errorinterfaceBar{bool: string;}constc: Bar[]=data.foo;// no error (even though the types don't match)constd: Bar[]=data.foo;// compilation error
data.json:
{
"foo": [
{
"bool": true,
"str": "123"
}
]
}
Expected behavior:
Both assignments to a and b shouldn't error. Assignments to c and d should both error.
Actual behavior:
It seems like the first assignment per type is always treated correctly by tsc while the latter ones are all compilation errors. This happens regardless whether the types actually match or not.
Removing the lines with errors results in successfull compilation even though the types don't match.
index.ts:8:7 - error TS2322: Type '{ bool: boolean; str: string; }[]' is not assignable to type 'Foo[]'.
8 const b: Foo[] = data.foo;
~
index.ts:15:7 - error TS2322: Type '{ bool: boolean; str: string; }[]' is not assignable to type 'Bar[]'.
15 const d: Bar[] = data.foo;
~
So, two things going wrong here. 1. We actually attempt to issue an error on both calls, but for the first, we refine the error location to a property in the json document (because we use fresh literal properties to refine error locations), then toss out the error because it's not for the right file.
2. There error is there at all because the array literal is preventing unfreshness from being propagated through it - so the outer object is unfreshened, but the array and its member objects are not.
It considers the JSON import to be a fresh object literal?
No, it's very explicitly unfreshed - but that unfreshing is mistakenly prevented from propagating down into the nested object within the array literal.
TypeScript Version: 3.8.0-dev.20191105, 3.7.2
Search Terms: JSON incorrect type checking
Code
data.json
:Expected behavior:
Both assignments to
a
andb
shouldn't error. Assignments toc
andd
should both error.Actual behavior:
It seems like the first assignment per type is always treated correctly by
tsc
while the latter ones are all compilation errors. This happens regardless whether the types actually match or not.Removing the lines with errors results in successfull compilation even though the types don't match.
Repo with example: https://github.com/Marik-D/typescipt-issue
tsc
output:tsconfig
:The text was updated successfully, but these errors were encountered: