例如我得到了这个fn:
const aCar = {type: 'car', wheels: 4};
const aBike = {type: 'bike', wheels: 2};
const fn = (type:'a'|'b'):Car|Bike => {
if(type === `a`) return aCar;
if(type === `b`) return aBike;
}
问题是返回始终是Bike
或Car
,与类型无关。我想强制说,当类型为a
时,返回的格式始终为Car
,而b
的格式为Bike
。
这可能吗?
某事very close:
// @flow
const items = [1, 2, 3];
type FirstType = ((_: 'a') => number) & ((n: 'b') => Array<number>);
const first: FirstType = (n):any => {
if (n === "a") {
return items[0];
} else if(n === 'b') {
return items;
}
}
const a: number = first('a');
const b: Array<number> = first('b');
由于