TypeScript 3.9 이상에서는이 구조가 완벽하게 잘 작동했습니다.
type keys =
| 'one'
| 'another'
| 'yet_another';
type variables = {
'another': { count: number }
'yet_another': { count: number, total: number }
};
export type TFunction = <T extends keys>(
key: T,
...args: T extends keyof variables ? [variables[T]] : []
) => string;
함수에 전달되는 인수의 유효성을 검사 할 수 있습니다.
t('one');
t('another', {count: 1});
그러나 Typescript 4의 최신 업데이트로 인해이 구성은 더 이상 작동하지 않습니다.
Expected 2 arguments, but got 1.
t('one');
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/interfaces.d.ts
...args: T extends keyof variables ? [variables[T]] : [];
Arguments for the rest parameter 'args' were not provided.
지금 그것을 설명하는 방법에 대한 아이디어가 있습니까?
알겠습니다. 문제를 찾았습니다. 문제는 필요에 따라 변수가있는 함수로 형변환하는 데있었습니다.
그래서 TS4는 결국 더 좋아졌고 이제 이것을 잡았습니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다