特定类型与接口[键:字符串]:TypeScript中的任意接口不兼容
我不知道为什么特定类型与接口[键:字符串]:TypeScript中的任意接口不兼容,typescript,Typescript,我不知道为什么MyParams与cloudFunctions数组中的Params不兼容。TS表示:“类型'Params'中缺少属性'blabla',但类型'MyParams'中需要属性'blabla' //基本声明 接口参数{ [键:字符串]:任何 } 接口请求函数{ 参数:T } //我的职能 类型MyParams={ 布拉布拉:字符串 } const myFunction=async(请求:RequestFunction)=>{} //出口一切 const cloudFunctions:[s
MyParams
与cloudFunctions
数组中的Params
不兼容。TS表示:“类型'Params'中缺少属性'blabla',但类型'MyParams'中需要属性'blabla'
//基本声明
接口参数{
[键:字符串]:任何
}
接口请求函数{
参数:T
}
//我的职能
类型MyParams={
布拉布拉:字符串
}
const myFunction=async(请求:RequestFunction)=>{}
//出口一切
const cloudFunctions:[string,(请求:RequestFunction)=>any][][
['my-function',myFunction],
]
完全正确
request:RequestFunction
不能保证blabla
,因为myFunction
需要它,因为blabla
在Params
中是可选的
如果合适的话,你也可以在你的类型中选择它
类型MyParams={
布拉布拉?:字符串
}
如果需要blabla
,则需要在cloudFunctions
签名中指定添加any
或所有类型
const cloudFunctions:Array any]>=[
['my-function',myFunction],
]
问题的来源是
string,(request:RequestFunction)=>any
。这意味着第二个参数可以在没有blabla
的情况下使用
cloudFunctions[0][1]({params:{}});//作品
但是在myFunction
blablabla
的情况下,这种回调应该失败
cloudFunctions[0][1]({params:{blabla:'string'}});//正确用法
此问题会导致错误。完全正确
request:RequestFunction
不能保证blabla
,因为myFunction
需要它,因为blabla
在Params
中是可选的
如果合适的话,你也可以在你的类型中选择它
类型MyParams={
布拉布拉?:字符串
}
如果需要blabla
,则需要在cloudFunctions
签名中指定添加any
或所有类型
const cloudFunctions:Array any]>=[
['my-function',myFunction],
]
问题的来源是
string,(request:RequestFunction)=>any
。这意味着第二个参数可以在没有blabla
的情况下使用
cloudFunctions[0][1]({params:{}});//作品
但是在myFunction
blablabla
的情况下,这种回调应该失败
cloudFunctions[0][1]({params:{blabla:'string'}});//正确用法
此问题导致错误。我已经更新了答案,如果需要-您需要在
request:RequestFunction
或使用request:RequestFunction
中指定答案。我已经更新了答案,如果需要-您需要在request:RequestFunction
或使用request:RequestFunction
中指定答案。
// Basic declarations
interface Params {
[key: string]: any
}
interface RequestFunction<T extends Params = Params> {
params: T
}
// My function
type MyParams = {
blabla: string
}
const myFunction = async (request: RequestFunction<MyParams>) => { }
// Export everything
const cloudFunctions: [string, (request: RequestFunction) => any][] = [
['my-function', myFunction],
]