学习---typescript的类型复用
1、复用type类型
type Point =
{
x: number;
y: number;
};
type Coordinate = Point &
{
z: number;
};
2、复用interface类型
interface Point
{
x: number;
y: number;
};
interface Coordinate extends Point
{
z: number;
}
3、interface 复用新增属性
interface Props {
a: string;
b: string;
c: string;
}
interface Props1 {
a: string;
b: string;
e: string;
}
可以改写为:
interface Props
{
a: string;
b: string;
c: string;
}
interface Props1 extends Omit<Props, 'c'> {
e: string;
}
interface Props1 extends Pick<Props, 'a' | 'b'> {
e: string;
}
omit的作用用于排除选择类型中的属性,在这个例子意思就是,复用a/b属性,去掉c属性
Pick 的作用是用于选择,所需要的属性