【发布时间】:2020-12-27 10:08:26
【问题描述】:
我正在尝试从一个对象创建一个模板文件,其中键可以是字符串或返回字符串的函数:
export const createDynamicTemplate = (
templateParams: CreateDynamicTemplateParams
) => {
const { template, projectPath = '', param = '' } = templateParams
const updatedTemplateArr = Object.keys(template).map((key: string) => {
return {
[key]: {
filePath: `${projectPath}/${key}`,
template: typeof template[key] === 'function' ?
template[key](param) : template[key],
},
}
})
const updatedTemplate = Object.assign({}, ...updatedTemplateArr)
return updatedTemplate
}
我的界面是:
export interface TemplateObject {
[key: string]: string
}
export interface FunctionalTemplateObject {
[key: string]: (param: string) => void
}
export interface CreateDynamicTemplateParams {
template: FunctionalTemplateObject | TemplateObject
projectPath: string
param: string
}
它一直在createDynamicTemplate 中抛出此错误:
此表达式不可调用。 并非所有类型为 'string | ((param: string) => void)' 是可调用的。 类型“字符串”没有调用签名。
我在这里做错了什么?
【问题讨论】:
标签: node.js typescript types