![](/img/trans.png)
[英]How to properly use typescript module augmentation for material-ui components?
[英]Changing interface into type with module augmentation in Typescript
使用 TS 扩充模块时是否可以将interface
更改为type
? 我想用我自己的一些属性来增强 Material-UI 中的主题。 我创建了createPalette.d.ts
文件:
import '@material-ui/core/styles/createPalette';
declare module '@material-ui/core/styles/createPalette' {
export interface PaletteOptions {
neutral?: {
moderate: string;
dark: string;
};
}
export interface Palette {
neutral: {
moderate: string;
dark: string;
};
}
}
PaletteOptions
和Palette
接口的内容几乎相同,所以我想通过提取neutral
作为外部类型来简化它,并提出如下内容:
import '@material-ui/core/styles/createPalette';
type CustomPalette = {
neutral: {
moderate: string;
dark: string;
};
};
declare module '@material-ui/core/styles/createPalette' {
export interface PaletteOptions extends Partial<CustomPalette> {}
export interface Palette extends CustomPalette {}
}
它可以工作,但是PaletteOptions
和Palette
现在被认为是“空的”,并且由于no-empty-interfaces
规则而出现错误。 在点击自动修复 ESLint 后将其更改为类型:
import '@material-ui/core/styles/createPalette';
type CustomPalette = {
neutral: {
moderate: string;
dark: string;
};
};
declare module '@material-ui/core/styles/createPalette' {
export type PaletteOptions = Partial<CustomPalette>;
export type Palette = CustomPalette;
}
没有 ESLint 错误,但现在模块扩充不起作用,因为 Material-UI 类型PaletteOptions
和Palette
声明为接口,而 TS 忽略了我的声明。 我现在有什么选择? 有什么办法可以诱使 TS 接受PaletteOptions
和Palette
是类型,还是我应该只是 go 和eslint-ignore
在该规则上?
似乎该规则假设您没有进行扩充,并且禁用它是安全的,因为您需要另一个界面来扩充现有界面。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.