interface MyProps {
x: number;
y: string;
}
const myVar: MyProps = {
x: 1,
y: '2',
};
function getMyValue(prop?: keyof MyProps) {
if (prop) {
return myVar[prop];
}
return myVar;
}
const x = getMyValue('x');
const y = getMyValue('y');
const val = getMyValue();
这个时候,x 的类型 is 'string' | 'number' | MyProps, 但是我预期的类型 x is 'number'. y is 'string', and val is MyProps. 怎么做比较优雅