Luk*_*ach 6 interface object typescript spread-syntax
在JavaScript中,可以使用扩展语法将对象扩展到另一个对象:
const a = {one: 1, two: 2}
const b = {...a, three: 3} // = {one: 1, two: 2, three: 3}
Run Code Online (Sandbox Code Playgroud)
有没有办法以这种方式将打字稿界面传播到另一个界面?
interface IA {
one: number;
two: number;
}
interface IB {
...IA; // Does not work like this
three: number;
}
Run Code Online (Sandbox Code Playgroud)
这样生成的界面IB看起来像这样:
{
one: number;
two: number;
three: number;
}
Run Code Online (Sandbox Code Playgroud)
你可以使用继承来做到这一点:
interface IA {
one: number;
two: number;
}
interface IC {
other: number;
four: number;
}
interface IB extends IA, IC {
three: number;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1323 次 |
| 最近记录: |