Skip to content

Literal type false unexpectedly widens into type boolean #25474

@cliffkoh

Description

@cliffkoh

TypeScript Version: 3.0.0-dev.20180705

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

Code

type Foo = FooBase | FooArray;
type FooBase = string | false;
type FooArray = FooBase[];

declare let foo1: Foo;
declare let foo2: 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

Assignees

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions