TypeScript Version: 2.3.4
Code
functionfn(arg : ('a'|'b')[]){}// ONE// type checked okayfn(['a','b'])// TWOconstx=['a','b']fn(x)// throws type check error:// Argument of type 'string[]' is not assignable to parameter of type '("a" | "b")[]'. Type 'string' is not assignable to type '"a" | "b"'.Expected behavior:
calling case TWO should pass the type check fine
Actual behavior:
case TWO throws type check error:
Argument of type 'string[]' is not assignable to parameter of type '("a" | "b")[]'. Type 'string' is not assignable to type '"a" | "b"'.
typescript automatically casts the variable x to type string[], and in doing so loses all information about its contents, which is why it fails the strict value check.
this is a problem because it means you can't pass reusable arrays without strictly typing the original variable, which causes problems when using generics (a bit of a contrived example, but still):
functionfn<T>(){returnfunction<TFieldextendskeyofT>(fields : TField[]){}}constStructType={a: 1,b: 2,}// have to manually define the same type as the inner generics, which kind of ruins encapsulation.constx : (keyoftypeofStructType)[]=['a','b']fn<typeofStructType>()(x)// other stuff...fn<typeofStructType>()(x)
TypeScript Version: 2.3.4
Code
Expected behavior:
calling case TWO should pass the type check fine
Actual behavior:
case TWO throws type check error:
Argument of type 'string[]' is not assignable to parameter of type '("a" | "b")[]'. Type 'string' is not assignable to type '"a" | "b"'.
typescript automatically casts the variable
xto typestring[], and in doing so loses all information about its contents, which is why it fails the strict value check.this is a problem because it means you can't pass reusable arrays without strictly typing the original variable, which causes problems when using generics (a bit of a contrived example, but still):