Typescript,是否可以在没有某种类型的情况下扩展接口


import React from "react";
interface a_to_e {
a?: string;
b?: string;
c?: string;
d?: string;
e?: string;
}
interface a_to_e_without_c extends a_to_e {
// I'd like to implement a~e without c
}
function Child(props: a_to_e_without_c) {
return (
<>
<div>child</div>
</>
);
}
function App() {
return (
<>
<Child c="I'd like to throw compile error," />
</>
);
}
export default App;

除了一些特殊类型之外,是否可以在typescript中扩展接口?

当然,它可以通过自定义异常来实现

但我想抛出编译错误

当我的一些同事使用属性为c.的Child组件时

有可能吗?

您可以使用typescript 的Omit实用程序

示例:

interface a {
a: string,
b: string,
c: string
}
type without_c = Omit<a, "c">;
const variable_with_c: without_c = {
a: "a",
b: "b",
c: "c" //compile error
}

最新更新