I can't make following function type checked without using any
somewhere.
export function makeToggleState<T, K extends keyof T>(obj: {new(): T}, prop: K) {
return (state: T, show: boolean|null = null) => {
if (show === null) {
state[prop] = !state[prop]
} else {
state[prop] = show
}
}
}
class State {
value = true
}
makeToggleState(State, 'value')
I am getting this error:
Type 'false' is not assignable to type 'T[K]'.
Type 'false' is not assignable to type 'T[string]'.
(parameter) prop: K extends keyof T
What is the best way to tell compiler I want K extends keyof T
and T[K] is boolean
?
There is no way to have this constraint in makeToggleState
declaration, but you can declare it for the function it returns, using intersection of T and mapped type T & {[n in K]: boolean}
for its state parameter.
export function makeToggleState<T, K extends keyof T>(obj: {new(): T}, prop: K) {
return (state: T & {[n in K]: boolean}, show: boolean|null = null) => {
if (show === null) {
state[prop] = !state[prop]
} else {
state[prop] = show
}
}
}
class State {
value = true;
name = 'q';
}
const toggleValue = makeToggleState(State, 'value');
const s = new State();
toggleValue(s)
const toggleName = makeToggleState(State, 'name'); // ok
// but does not compile when you try to use it
toggleName(s);
//Argument of type 'State' is not assignable to parameter of type 'State & { name: boolean; }'.
// Type 'State' is not assignable to type '{ name: boolean; }'.
// Types of property 'name' are incompatible.
// Type 'string' is not assignable to type 'boolean'.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments