Closed
Description
opened on Jun 9, 2017
TypeScript Version: 2.3.4
Code
function fn(arg : ('a' | 'b')[]) { }
// ONE
// type checked okay
fn(['a', 'b'])
// TWO
const x = ['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):
function fn<T>() {
return function<TField extends keyof T>(fields : TField[]) { }
}
const StructType = {
a: 1,
b: 2,
}
// have to manually define the same type as the inner generics, which kind of ruins encapsulation.
const x : (keyof typeof StructType)[] = ['a', 'b']
fn<typeof StructType>()(x)
// other stuff...
fn<typeof StructType>()(x)
Activity