Closed
Description
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.