为什么打字稿不允许我投射以下内容:
interface foo {
  foofoo: string
  feefee: string
  faafaa: 'red' | 'blue'
}
进入Record<string, string>?我怎样才能做到这一点?
我试过了:
string键入instanceOfFoo as Record<string, string>...没有任何快乐任何帮助表示赞赏
TypeScript 在这里发出错误信号并不是因为这两种类型不可相互分配,而是因为它们没有共同的属性,这几乎总是由错误造成的,并且错误上的说明相当清楚地说明了在这种情况下该怎么做那不是。
将“foo”类型转换为“Record”类型可能是一个错误,因为这两种类型都没有与另一种类型充分重叠。如果这是故意的,请首先将表达式转换为“未知”。
强调我的。
所以你需要做的是
declare const x: foo; // there exists a variable x of type foo.
const y = x as unknown as Record<string, string>; // cast to unknown, then to Record.
| 归档时间: | 
 | 
| 查看次数: | 6727 次 | 
| 最近记录: |