【发布时间】:2021-10-02 13:42:12
【问题描述】:
我正在使用一个具有类似 TS def 的模块:
// index.d.ts
interface SomeContext {
// ... other props
thing?: Record<string, any>;
}
当我们使用这个模块时,我们会注意 someContext.thing 被定义,所以当我们开始使用它时它永远不会被 undefined。我们想在我们的代码中给它我们自己的类型,因为我们会知道形状是什么,但必须执行以下操作才能让它通过 TS 构建:
const { thing: ourThing } = <{ thing: any }>someContext.thing;
这感觉像是错误的做法,因为我们失去了 TS 通常给我们的安全和 DX。
有没有更好的方法来解决这个问题?
【问题讨论】:
标签: javascript typescript types casting