小编Mat*_*rim的帖子

从界面中排除动态密钥

我正在尝试使用猫鼬模式进行更好的打字,但似乎该SchemaTypeOpts界面有一个[other: string]: any;,这阻止了 vcsode intellisense 帮助我。

为了解决这个问题,我尝试了以下方法,希望它能够识别动态属性创建的密钥(因为它的名称是other):

const x: Pick<SchemaTypeOpts<any>, Exclude<keyof SchemaTypeOpts<any>, "other">> = {
    bob: "", // expected an error here
    type: String
}
Run Code Online (Sandbox Code Playgroud)

但当Exclude接口具有动态属性时,它似乎并没有多大作用:/

例如:这有效:

interface test2 {
    x?: number,
    y?: string,
    z?: number,
}


const x: Pick<test2, Exclude<keyof test2, "z">> = {
    y: "",
    z: 5, // got error here because z is excluded
    bob: "" //got error here because bob is not in the interface
}
Run Code Online (Sandbox Code Playgroud)

这什么也不做:

interface …
Run Code Online (Sandbox Code Playgroud)

typescript

5
推荐指数
0
解决办法
1563
查看次数

标签 统计

typescript ×1