说我有一个方法的签名:
interface MyClass<T> {
data: T
}
create(options: {
fields: (string | { name: string, type?: string})[],
[key: string]: any
}): MyClassConstructor<T extends from_some_magic_extracting_fields_out>
在运行时会发生什么是在fields
中循环options
,如果它是一个字符串,将创建具有相同字符串值的属性名称,否则,从name
属性获取
我如何在Typescript界面中为我的JS提供intellisense?
修改1 :用法示例
window.RecordCreator = function(opts) {
var dataCfg = opts.fields.reduce(function(cfg, field) {
if (typeof field === 'string') {
cfg.push({ name: field, type: 'string' });
} else {
cfg.push({ name: field.name, type: field.type });
}
return cfg;
}, []);
// More stuff here to setup prototype for RecordConstructor
return function RecordConstructor() {
this.data = dataCfg.slice(0);
}
}