Javascript 流中具有混合值的对象属性赋值
遇到流问题,无法找到解决方法 最好用代码来描述:Javascript 流中具有混合值的对象属性赋值,javascript,flowtype,Javascript,Flowtype,遇到流问题,无法找到解决方法 最好用代码来描述: type Mytype = { propA?: string, propB?: number, }; type myTypeProps = $Keys<typeof MyType>; const source : Mytype = { propA: 'hello', propB: 10, }; const myFunc = (pro
type Mytype = {
propA?: string,
propB?: number,
};
type myTypeProps = $Keys<typeof MyType>;
const source : Mytype = {
propA: 'hello',
propB: 10,
};
const myFunc = (props: Array<myTypeProps>, ): Mytype => {
const dest:Mytype = {};
props.forEach(propType => {
dest[propType] = source[propType];
//Flow Error because of
//type mismatch: string cannot be assigned to number
});
return dest;
}
因此,明确说明键的作用是有效的,但当键被参数化时,就不起作用了
有什么想法吗
const dest:Mytype = {};
dest['propA'] = source['propA'];
dest['propB'] = source['propB'];
return dest;