TypeScript에서 형식 속성을 재정의하는 방법
예를 들어, 나는.
type Line = {
start: Point;
end: Point;
color: string; //'cyan'/'aquablue'/...
}
그러나 이제 선을 기준으로 새 선종류를 작성하여 색상을 숫자로 저장하려고 합니다.
type HexColorLine = Point & {
color: number;
}
HexColorPoint 유형은 다음과 같습니다.
{
start: Point;
end: Point;
color: number;
}
하지만 그것은
{
start: Point;
end: Point;
color: string | number;
}
짧은 구문으로 프롭 유형을 재정의하되 확장하지 않는 방법이 있습니까?이를 위해 완전히 새로운 유형을 정의해야 합니까?
도우미 유형 만들기:
type Overwrite<T, U> = Pick<T, Exclude<keyof T, keyof U>> & U;
용도:
type HexColorLine = Overwrite<Line, { color: number }>
TypeScript 3.5의 간단한 솔루션은 다음과 같습니다.
type HexColorLine = Omit<Line, 'color'> & {
color: number;
}
TypeScript 3.5 이상에서 를 사용할 수 있습니다.
유형:
type HexColorPoint = Omit<Line, "color"> & {
color: number;
}
인터페이스의 경우:
interface HexColorPoint extends Omit<Line, "color"> {
color: number;
}
현재 지원되지 않습니다.TypeScript에는 감산 유형 개념이 필요합니다.제안은 https://github.com/Microsoft/TypeScript/issues/12215 및 https://github.com/Microsoft/TypeScript/issues/4183 에 있습니다.
고치다
기준 유형 작성:
type LineBase = {
start: Point;
end: Point;
}
type LineBase = LineBase & {
color: string; //'cyan'/'aquablue'/...
}
생략을 사용하는 파블로의 답변을 확장하려면 다음을 수행하여 보다 일반적인 유틸리티 유형을 만들 수 있습니다.
type Overwrite<Base, Overrides> = Omit<Base, keyof Overrides> & Overrides
type HexColorLine = Overwrite<Line, { color: number }>
제공된 모든 키가 생략됩니다.Overrides
에서 타이핑합니다.Base
type을 입력한 다음 제공된 것과 결합합니다.Overrides
.
TL;DR:
type Omit<T, K> = Pick<T, Exclude<keyof T, K>>
type Override<T, U> = Omit<T, keyof U> & U
type ColorNumber = {
color: number;
}
type HexColorPoint = Override<
Line,
ColorNumber
> // --> {start: Point; end: Point; color: number}
당신이 하고 싶었을 거예요
type HexColorLine = Line & {
color: number;
}
대신에
type HexColorLine = Point /* <-- typo? */ & {
color: number;
}
Typescript > 2.8을 사용하여 다음과 같이 재정의할 수 있었습니다.
https://www.typescriptlang.org/docs/handbook/release-notes/typescript-2-8.html 에서:
생략 유형은 Pick<T, Exclude<T, K>>로 사소한 것이기 때문에 포함하지 않았습니다.
// So we define Omit -.-
type Omit<T, K> = Pick<T, Exclude<keyof T, K>>
// Sidenote:
// keyof is just getting all the keys for a given type separeted by |
// keyof Line --> 'start' | 'end' | 'color'
// And we define Override which unions the type without unwanted keys and the
// type defining the new keys
type Override<T, U> = Omit<T, keyof U> & U
// just define which properties you want to redefine
// and remove "Point &" as it will be done in the Override type
type HexColorLine = {
color: number;
}
type HexColorPoint = Override<
Line,
HexColorLine
> // --> {start: Point; end: Point; color: number}
시도해 보세요Override
에서utility-types
패키지 https://github.com/piotrwitek/utility-types#overwritet-u .그래서 미래에 여러분은 다른 멋진 도우미들을 사용하고 싶을지도 모릅니다.
언급URL : https://stackoverflow.com/questions/43080547/how-to-override-type-properties-in-typescript
'programing' 카테고리의 다른 글
ORA-12705: NLS 데이터 파일 또는 잘못된 환경에 액세스할 수 없습니다. (0) | 2023.07.03 |
---|---|
스프링 예약 작업과 스프링 배치 작업의 차이점은 무엇입니까? (0) | 2023.07.03 |
SQL Server에서 DF 제약 조건을 계속 생성하는 이유는 무엇입니까? (0) | 2023.07.03 |
선택 항목 내 SQL 사용자 정의 함수 (0) | 2023.07.03 |
오류: 모듈 파일의 최소 배포 대상은 ios8.3 v8.3입니다. (0) | 2023.07.03 |