티스토리 뷰
https://github.com/stephenh/ts-proto/pull/1034
proto 파일을 typescript 로 변환해주는 ts-proto 프로젝트에 기여했습니다.
protobuf 의 oneof field 를 변환한 결과로 union type 이 반환되는데, 이 타입을 지정할 수 있는 type helper 를 추가했습니다.
/** Extracts the specific type of a value type from a oneOf field */
export type OneOfValue<T, K extends OneOfCases<T>> = T extends {
$case: K;
[key: string]: unknown;
}
? T
: never;
이 helper 를 사용하면 아래와 같이 oneof 로 지정한 필드중 하나를 타입으로 지정할 수 있습니다.
// pass by argument
foo<P extends PaloadNames>(
payload: OneOfValue<LogPayload['payload'], P>
) {
const req: LogPayload = {
createdAt: new Date().toUTCString(),
payload,
};
send(req);
}
// Usage
// Good
foo({
$case: 'login',
login: {
accountId: 'accountId',
},
});
// Build Fail because case and field is not matched
// Argument of type '{ $case: string; login: { accountId: string; }; }' is not assignable to parameter of type 'never'.
foo({
$case: 'logout',
login: {
accountId: 'accountId',
},
});
'개발 > 일상' 카테고리의 다른 글
windows10 wsl nu shell 적용하기 (0) | 2023.07.31 |
---|---|
Unity Oculus Quest 2 개발 환경 만들기 (0) | 2023.07.30 |
Window10 OpenSSL 설치 & 환경변수 등록하기 (0) | 2023.02.21 |
boost strand 사용해보기 (0) | 2023.02.21 |
jupyter notebook 에 anaconda 로 독립된 환경 만들기 (0) | 2022.12.11 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
TAG
- hole-punching
- 코어 남기기
- Obstacle Avoidance
- set value
- Keyboard to Mouse Click
- mysql
- 봄날에 스케치
- chrome-extension
- boost
- RVO
- Quest2
- 최솟값 찾기
- ad skip
- print shared_ptr class member variable
- 영상 픽셀화 하기
- 카카오
- Reciprocal n-body Collision Avoidance
- 포맷해서 boost 새로 까는 김에
- 면접
- Golang
- 에러 위치 찾기
- 클래스 맴버 변수 출력하기
- red underline
- cockroach db
- 로그인 과정에서 오류가 발생했습니다.
- SuffixArray
- shared_from_this
- C++
- 잘못된 빨간줄
- Visual Studio
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함