TypeScript中对象属性内createPath方法的参数类型推断及存在性确认问题
TypeScript中对象属性内createPath方法的参数类型推断及存在性确认问题
我现在在创建这样一个对象:
type AllLiterals = "one" | "two" | "three"; type ObjectConfig<T=unknown> = { path: string; value: AllLiterals; createPath?: (params:T)=>string } const config: Record<AllLiterals,ObjectConfig> = { one: { path: "one-path", value: "one", }, two: { path: "two-path", value: "two", createPath: (a:string)=>`two-path/${a}` }, three: { path: "three-path", value: "three", createPath: ({a, b}:{a:string,b:string})=>`three-path/${a}/${b}` } }
当我尝试调用createPath时,TypeScript无法正确推断出参数的类型,请问该怎么解决这个问题?
另外我还需要实现:当我在config的某个属性里定义了createPath方法时,TypeScript能明确知道这个方法是存在的,这样我就不需要先检查它是否存在,直接就能调用它。
备注:内容来源于stack exchange,提问作者Ako
相关产品推荐
相关产品推荐

