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
State and State aren’t substitutable for the other.
in out이 없어도 아래는 false다
type B = State extends State ? true : false
type B = State extends State ? true : false
구조적 타이핑에서는 invariant, variant, coVariant를 명시하지 않아도 알아서 추론된다.
이 키워드를 사용하는 이유는 뭔가?
코드를 보는 사람에게 명시적으로 알려주기 위함이다.
속도와 정확성을 위해서다. 기존에는 공변, 반공변을 추론하지만 명시적으로 적으면 추론하는 비용을 아낄 수 있다.
그리고 공변과 반공변을 정확히 계산할 수 없는 경우도 있다고 한다.(? 명확하지않음) 타입스크립트 공식문서에는 다음과 같은 예시가 있다.
계산에서 정확하게 해결할 수 없는 순환을 발견하여 유형의 분산에 대한 명확한 답이 없는 경우가 종종 있습니다.
typeFoo<T>={x: T;f: Bar<T>;}typeBar<U>=(x: Baz<U[]>)=>void;typeBaz<V>={value: Foo<V[]>;}declareletfoo1: Foo<unknown>;declareletfoo2: Foo<string>;foo1=foo2;// Should be an error but isn't ❌foo2=foo1;// Error - correct ✅
The text was updated successfully, but these errors were encountered:
템플릿 예시입니다.
title:
full_path: src/pages/.md
TypeScript: Documentation - TypeScript 4.7
기존 Typescript는 공변과 반공변을 명시적으로 적을 수 있는 키워드가 없었다.
T에 대해 공변일때는 다음과 같이 쓰면된다.
반공변일때는 in을 쓰자
in , out 그저 input과 output으로 생각해보자
만약 다음과 같이 T에 대해서 input, output에 둘다 쓰이는 타입이라면 in out을 둘다 쓴다.
이런 것은 inVariant라고 부른다.
State and State aren’t substitutable for the other.
in out이 없어도 아래는 false다
type B = State extends State ? true : false
type B = State extends State ? true : false
구조적 타이핑에서는 invariant, variant, coVariant를 명시하지 않아도 알아서 추론된다.
이 키워드를 사용하는 이유는 뭔가?
그리고 공변과 반공변을 정확히 계산할 수 없는 경우도 있다고 한다.(? 명확하지않음) 타입스크립트 공식문서에는 다음과 같은 예시가 있다.
The text was updated successfully, but these errors were encountered: