class Base<T> {
public state = {} as T;
public getState(): T {
return this.state
}
public setState(v: T) {
this.state = v
}
}
interface DogProps {
name: 'hello';
age: 123;
}
class Dog extends Base<DogProps> {
public sayName() {
console.log('name: ', this.state.name);
}
public sayAge() {
console.log('age: ', this.state.age);
}
}
function test<U, T extends Base<U>>(Cor: new () => T): [U, T] {
const dog = new Cor();
const state = dog.getState();
return [state, dog];
}
const [state1, dog1] = test(Dog); // state1 is unknow
const [state2, dog2] = test<DogProps, Dog>(Dog); // verbose but right
I am newbe in typescript.
I thought the code I wrote was correct. However, it does not behave as expected.
Why is the type of state1 unknown?
Is there a way to determine the correct type without using test<DogProps, Dog>(Dog)
?
Many thanks!!!