所以我们有两个功能:
const car = ():Car => {return {type: `car`, wheels: 4}};
const house = ():House => {return {type: `house`, rooms: 3}};
所以我有一张地图:
const map = {car, house}
现在我有一个基于字符串的函数调用这样的汽车或自行车:
type myType = (type:string):any;
const get:myType = (type) => {
return map[type]();
};
现在我调用函数:
const myVar = get('car');
现在我想了解myVar
是Car
类型。是否可能,如果可以,怎么样?
我想将这些信息保存在地图中,而不是执行以下操作:
type carType = (type:'car'):Car;
type houseType = (type:'house'):House;
const get:carType|houseType = (type) => {
return map[type]();
};
类似于https://flow.org/en/docs/types/utilities/#toc-keys但是对于值。这似乎也很相关:https://flow.org/en/docs/types/utilities/#toc-objmap