Typescript: Set and Map don't infer own types (regression)

Created on 10 Jan 2018  路  2Comments  路  Source: microsoft/TypeScript

20606 should be reverted for now.

TypeScript Version: master

Code

new Set([]);

Expected behavior:

Set<never>

Actual behavior:

Set<never | ReadonlyArray<never> | undefined>

Related:

Bug Fixed

Most helpful comment

This actually looks to be a regression caused by #19745. A simple repro:

declare function foo<T>(a?: ReadonlyArray<T>): T;
let x = foo([]);  // never | ReadonlyArray<never> | undefined

The issue is that when inferring from Array<T> to ReadonlyArray<T> we structurally relate the types, which produces a number of secondary inferences. Usually these secondary inferences are suppressed by a higher priority inference of never, but with #19745 that doesn't happen (because inferences from an implicit never[] are given even lower priority).

All 2 comments

This actually looks to be a regression caused by #19745. A simple repro:

declare function foo<T>(a?: ReadonlyArray<T>): T;
let x = foo([]);  // never | ReadonlyArray<never> | undefined

The issue is that when inferring from Array<T> to ReadonlyArray<T> we structurally relate the types, which produces a number of secondary inferences. Usually these secondary inferences are suppressed by a higher priority inference of never, but with #19745 that doesn't happen (because inferences from an implicit never[] are given even lower priority).

This regression, it happens with common cases and makes a large impact, shouldn't be released with the next stable version. Should be fixed by no later than the next releasing.

Was this page helpful?
0 / 5 - 0 ratings