如何在TypeScript中重写此ES6代码?
我对打字很不熟悉。我发现很难将一些ES6代码重写为TypeScript 例如,如何将其重写为typescript如何在TypeScript中重写此ES6代码?,typescript,Typescript,我对打字很不熟悉。我发现很难将一些ES6代码重写为TypeScript 例如,如何将其重写为typescript const actionTypes = [ 'SAMPLE', ] export default actionTypes.reduce((obj, str) => { const mirror = { [str]: str, } return { ...obj, ...mirror } }, {}) 我试过这个 const actionTypes:
const actionTypes = [
'SAMPLE',
]
export default actionTypes.reduce((obj, str) => {
const mirror = {
[str]: str,
}
return { ...obj, ...mirror }
}, {})
我试过这个
const actionTypes: String[] = ['SAMPLE']
const map: Object = actionTypes.reduce((obj: Object, str: String) => {
const mirror: Object = {
[str]: str,
}
return Object.assign({}, obj, mirror)
}, {})
但是,它在[str]:str
处抛出错误
[ts]计算属性名称必须为“字符串”、“数字”、“符号”或“任意”类型
(参数)str:String
我做错了什么?TS是否支持扩展运算符?编辑:Rest/spread属性{…obj,…mirror}
使用TypeScript 2.1。看
类型
字符串
带有小写字母“s”。不要使用类型对象
。可以对空对象的类型使用{}
但是,在您的例子中,actionTypes
被推断为string[]
。然后reduce
函数的参数obj
和str
也被推断为string
const actionTypes = ['SAMPLE']
const map = actionTypes.reduce((obj, str) => {
const mirror = {
[str]: str,
}
return Object.assign({}, obj, mirror)
}, {})
注意:Rest/spread属性{…obj,…mirror}
不是来自ES6,而是来自ES7
使用for of循环的经典方式更具表现力,性能更佳:
const actionTypes = ['SAMPLE'],
map = {}
for (const name of actionTypes)
map[name] = name
使用TypeScript 2.1编辑:Rest/spread属性{…obj,…mirror}
。看
类型
字符串
带有小写字母“s”。不要使用类型对象
。可以对空对象的类型使用{}
但是,在您的例子中,actionTypes
被推断为string[]
。然后reduce
函数的参数obj
和str
也被推断为string
const actionTypes = ['SAMPLE']
const map = actionTypes.reduce((obj, str) => {
const mirror = {
[str]: str,
}
return Object.assign({}, obj, mirror)
}, {})
注意:Rest/spread属性{…obj,…mirror}
不是来自ES6,而是来自ES7
使用for of循环的经典方式更具表现力,性能更佳:
const actionTypes = ['SAMPLE'],
map = {}
for (const name of actionTypes)
map[name] = name
我会使用泛型:
function createMirror<T>(actionTypes: string[]) {
let obj = {};
actionTypes.forEach((str) => {
const mirror = {
[str]: str,
}
obj = Object.assign({}, obj, mirror);
});
return <T>obj;
};
interface MyActionTypes {
SAMPLE: string
}
const myActionTypes = [
'SAMPLE',
];
var map = createMirror<MyActionTypes>(myActionTypes);
函数createMirror(actionTypes:string[]{
设obj={};
actionTypes.forEach((str)=>{
常量镜像={
[str]:str,
}
obj=Object.assign({},obj,mirror);
});
返回obj;
};
接口MyActionType{
示例:字符串
}
常量myActionTypes=[
“样本”,
];
var map=createMirror(myActionTypes);
如果使用Object.assign
您将失去TypeScript的优点:
我会使用泛型:
function createMirror<T>(actionTypes: string[]) {
let obj = {};
actionTypes.forEach((str) => {
const mirror = {
[str]: str,
}
obj = Object.assign({}, obj, mirror);
});
return <T>obj;
};
interface MyActionTypes {
SAMPLE: string
}
const myActionTypes = [
'SAMPLE',
];
var map = createMirror<MyActionTypes>(myActionTypes);
函数createMirror(actionTypes:string[]{
设obj={};
actionTypes.forEach((str)=>{
常量镜像={
[str]:str,
}
obj=Object.assign({},obj,mirror);
});
返回obj;
};
接口MyActionType{
示例:字符串
}
常量myActionTypes=[
“样本”,
];
var map=createMirror(myActionTypes);
如果使用Object.assign
您将失去TypeScript的优点:
但是如果您在编译时了解
actionTypes
的内容,那么您应该只声明映射:const-map={SAMPLE:'SAMPLE'}
。更简洁。但是如果您在编译时了解actionTypes
的内容,那么您应该只声明map:const map={SAMPLE:'SAMPLE'}
。更简洁。