[英]TypeScript declaration merging not works?
I want to add a new member to an existing interface.我想向现有接口添加新成员。 But typescript keeps throwing errors.
但是 typescript 不断抛出错误。
// foo.js
export interface IOption {
xOffset: number
}
import {IOption} from 'foo';
// I want to add a `yOffset` to IOption, but this won't work.
// I got an error: `Import declaration conflicts with local declaration of 'IOptions'`
interface IOption {
yOffset: number
}
What's the problem with my code?我的代码有什么问题?
You need to declare the interface for the same module.您需要为同一模块声明接口。
import {IOption} from 'foo';
declare module 'foo' {
interface IOption {
yOffset: number
}
}
You cannot extend an existing interface, but you can create another one with the same name, by using a trick:您不能扩展现有接口,但可以使用一个技巧创建另一个具有相同名称的接口:
import * as foo from 'foo';
interface IOption extends foo.IOptions {
yOffset: number
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.