In this case the type of state is correct.
export type Flatten<T> = T extends infer U ? { [K in keyof U]: U[K] } : never
class Foo<S> {
state?: Partial<S>
}
class Bar<S> extends Foo<Flatten<S & { b: string }>> {
async getInitialState(initialState: S) {
return {
...initialState,
b: 'bar'
}
}
}
const initialState = {
a: 'baz'
}
class Baz extends Bar<typeof initialState> {
}
let baz = new Baz()
baz.state
// Partial<{
// a: string;
// b: string;
// }> | undefined
but in this case, the type of state will be override when assign a new value
class Baz extends Bar<typeof initialState> {
state = initialState
}
let baz = new Baz()
baz.state
// {
// a: string;
// }
I don't want to change the type of state in case 2. how should i do?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…