我在这里想要实现的是从数组生成的对象的智能感知/自动完成-像Redux的Action Creator一样string[],可以将字符串数组()简化为具有形状的对象{ [string]: string }。
例如:
const a = ['ONE', 'TWO', 'THREE'];
const b = a.reduce((acc, type) => ({ ...acc, [type]: type }), {});
console.log(b);
// Output: b = { ONE: 'ONE', TWO: 'TWO', THREE: 'THREE' };
Run Code Online (Sandbox Code Playgroud)
我已经使用以下方法设法使TypeScript对此有所了解。TypeScript知道键是字符串,但不知道它们是什么。
interface ITypesReturnObject { [s: string]: string }
Run Code Online (Sandbox Code Playgroud)
有没有人想出一种方法来通知TypeScript对象上的键等于数组中的字符串?
任何帮助将不胜感激。