You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
typeLength<Textendsreadonlyany[]>=Textends{length: infer L}
? L
: never;
TypeScript의 조건부 타입과 infer 키워드를 사용하여 배열(또는 튜플)의 길이를 추론하는 방법입니다.
T extends { length: infer L } > ... 조건부 타입의 형태로 작성된 이 부분은 T가 { length: infer L }라는 구조와 일치하는지 확인합니다. { length: infer L }는 T 타입이 length라는 속성을 가지고 있는지를 확인하고, infer 키워드를 사용하여 그 속성의 타입을 L로 추론(infer)합니다.
그 후 추론된 결과를 반환합니다.
Solution
/* _____________ 여기에 코드 입력 _____________ */typeLength<Textendsreadonlyany[]>=T["length"];/* _____________ 테스트 케이스 _____________ */importtype{Equal,Expect}from"@type-challenges/utils";consttesla=["tesla","model 3","model X","model Y"]asconst;constspaceX=["FALCON 9","FALCON HEAVY","DRAGON","STARSHIP","HUMAN SPACEFLIGHT",]asconst;typecases=[Expect<Equal<Length<typeoftesla>,4>>,Expect<Equal<Length<typeofspaceX>,5>>,// @ts-expect-errorLength<5>,// @ts-expect-errorLength<"hello world">];
The text was updated successfully, but these errors were encountered:
문제 설명
배열(튜플)을 받아 길이를 반환하는 제네릭 Length를 구현하세요.
예시:
풀이
가장 쉽게 풀어 낼 수 있는 방법입니다.
TypeScript의 조건부 타입과 infer 키워드를 사용하여 배열(또는 튜플)의 길이를 추론하는 방법입니다.
T extends { length: infer L } > ...
조건부 타입의 형태로 작성된 이 부분은 T가 { length: infer L }라는 구조와일치
하는지 확인합니다.{ length: infer L }
는 T 타입이 length라는 속성을 가지고 있는지를 확인하고, infer 키워드를 사용하여 그 속성의 타입을L로 추론(infer)
합니다.그 후 추론된 결과를 반환합니다.
Solution
The text was updated successfully, but these errors were encountered: