Skip to content

Boolean inference is too strict #31249

Closed
Closed
@hipolipolopigus

Description

@hipolipolopigus

TypeScript Version: 3.5.0-dev.20190503

Search Terms: Boolean inference true false

Code

type State<TStateKeys extends string = string> = {
	[k in TStateKeys]: boolean | number | string
};

class Foo<TState extends State> {
	constructor(public state: TState) {}
}

const obj = new Foo({
	a: 1,
	b: "string",
	c: true,
	d: false,
	// Workaround
	e: !!false
});

obj.state.a = 2; // No error
obj.state.b = "another string"; // No error

// Accepts only `false`, should accept either
obj.state.c = false; // TS2322: Type 'false' is not assignable to type 'true'.

// Accepts only `true`, should accept either
obj.state.d = true; // TS2322: Type 'true' is not assignable to type 'false'.

// Accepts either boolean
obj.state.e = true; // No error

Expected behavior: obj.state.c and obj.state.d accept either boolean.

Actual behavior: obj.state.c and obj.state.d are hard-typed to true and false, respectively.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions