Skip to content

Boolean inference is too strict #31249

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
hipolipolopigus opened this issue May 4, 2019 · 2 comments
Closed

Boolean inference is too strict #31249

hipolipolopigus opened this issue May 4, 2019 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@hipolipolopigus
Copy link

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.

@ahejlsberg
Copy link
Member

This issue has the same root cause as #30390, so I'm marking it as a duplicate.

@ahejlsberg ahejlsberg added the Duplicate An existing issue was already created label May 4, 2019
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants