Typescript: 3.4 regression on type inference with union typed array spreads

Created on 2 Apr 2019  路  3Comments  路  Source: microsoft/TypeScript

Reproduced with TS 3.4.1 and [email protected].

Search Terms:

Union typed array spreads used to work with TS 3.3 but require explicit types with TS 3.4.

Code

Works as expected on both 3.3 and 3.4:

declare function concat<T>(...args: (T | T[])[]): T[]
concat([1, 2, 3], 4)

Works on 3.3, breaks under 3.4 and @next:

export type Maybe<T> = T | undefined
declare function concatMaybe<T>(...args: (Maybe<T> | Maybe<T>[])[]): T[]

concatMaybe([1, 2, 3], 4)

Unexpected error

error TS2345: Argument of type '4' is not assignable to parameter of type 'number[] | (number[] | undefined)[] | undefined'.

This _does_ compile under 3.4, fwiw:

concatMaybe<number>([1, 2, 3], 4)

Related Issues:

30694

Bug Fixed

Most helpful comment

@ahejlsberg I don't know if you and your team get this enough, but THANK YOU FOR TYPESCRIPT!

You and your team are doing great work. Kudos!

All 3 comments

@ahejlsberg I don't know if you and your team get this enough, but THANK YOU FOR TYPESCRIPT!

You and your team are doing great work. Kudos!

@mceachen Thanks, much appreciated!

For real, TypeScript is the closest I have ever felt to expressing thought into code at the speed I can think it.

Was this page helpful?
0 / 5 - 0 ratings