Skip to content

Literal type false unexpectedly widens into type boolean #25474

Description

@cliffkoh

TypeScript Version: 3.0.0-dev.20180705

Search Terms: false/true loosened/expanded/loses specificity into boolean

Code

typeFoo=FooBase|FooArray;typeFooBase=string|false;typeFooArray=FooBase[];declareletfoo1: Foo;declareletfoo2: Foo;foo1=[...Array.isArray(foo2) ? foo2 : [foo2]];

Expected behavior:
No error.. In the else clause, [foo2], it retains string | false so Array<string | false>. Logically since everything is taking place in the typings layer, foo2 should still be strongly bounded to false and will never be true.

Actual behavior:
Errors. In the else clause, [foo2], it expanded/loosened to string | boolean so Array<string | boolean>.

tmp.ts:7:1 - error TS2322: Type '(string | boolean)[]' is not assignable to type 'Foo'.
Type '(string | boolean)[]' is not assignable to type 'FooBase[]'.
Type 'string | boolean' is not assignable to type 'FooBase'.
Type 'true' is not assignable to type 'FooBase'.
7 foo1 = [...Array.isArray(foo2) ? foo2 : [foo2]];
~~~~

Playground Link:Link

Related Issues:

Metadata

Metadata

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions