例如:
declare class Animal<State = any>{
life: number
state: State
setState(newState: State): void;
}
declare class Dog<state = any> extends Animal<state>{
name: string
}
declare class State {
synchronize: any
}
class Mystate extends State {
MyBalabalaState: string
}
class MyOtherstate extends State {
MyBalabalaState: string
}
class Labrador<Mystate=any> extends Dog<Mystate> {
balbala: string
}
class ChineseDog<MyOtherstate=any> extends Dog<MyOtherstate> {
balbala: string
}
function test() {
let mydog = new Labrador<Mystate>();
let mydog2 = new ChineseDog<MyOtherstate>();
let mydogState = new Mystate();
mydog.setState(mydogState);
let myDogOtherState=new MyOtherstate();
mydog2.setState(myDogOtherState)
//我的目的是想在循环泛型的时候能点出具体的提示
//可以实现吗?不知道怎么搞了。
//这里 map 要怎么定义才能获得 mydog 里面具体的 state 啊?
let map=new Map<string,Dog<State>>();//这样定义好像不行,在循环的时候不能获取属性
map.set("dog1",mydog);
map.set("dog2",mydog2);
//还有,这个循环不能指定具体类型吗?
for (const dog of map.values()) {
//我想在写代码的阶段就能获取到具体的类型,可以实现吗?
}
}