Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在TypeScript中重写此ES6代码?_Typescript - Fatal编程技术网

如何在TypeScript中重写此ES6代码?

如何在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:

我对打字很不熟悉。我发现很难将一些ES6代码重写为TypeScript

例如,如何将其重写为typescript

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'}
。更简洁。