programing

TypeScript 유형 정의에서 앰퍼샌드(&)는 무엇을 의미합니까?

css3 2023. 3. 26. 11:41

TypeScript 유형 정의에서 앰퍼샌드(&)는 무엇을 의미합니까?

이 유형의 정의 파일의 60359 행에는 다음과 같은 선언이 있습니다.

type ActivatedEventHandler = (ev: Windows.ApplicationModel.Activation.IActivatedEventArgs & WinRTEvent<any>) => void;

의 개요&이 문맥에서 signil은 무엇을 의미합니까?

&유형 위치에서 교차 유형을 의미합니다.

교차로 유형에 대한 타이프스크립트 문서에서 추가 정보:

https://www.typescriptlang.org/docs/handbook/2/objects.html#intersection-types

위에 링크된 문서에서 인용:

교차로 유형은 결합 유형과 밀접한 관련이 있지만 매우 다르게 사용됩니다.교차로 유형은 여러 유형을 하나로 결합합니다.이를 통해 기존 유형을 추가하여 필요한 모든 기능을 갖춘 단일 유형을 얻을 수 있습니다.예를 들어 [Person & Serializable & Loggable]는 모두 [Person]및 [Serializable]및 [Loggable]타입입니다.즉, 이 유형의 객체는 세 가지 유형의 모든 멤버를 가집니다.

예를 들어 일관된 오류 처리를 가진 네트워킹 요청이 있는 경우 오류 처리를 고유한 유형으로 분리하여 단일 응답 유형에 해당하는 유형과 병합할 수 있습니다.

interface ErrorHandling {
  success: boolean;
  error?: { message: string };
}

interface ArtworksData {
  artworks: { title: string }[];
}

interface ArtistsData {
  artists: { name: string }[];
}

// These interfaces are composed to have
// consistent error handling, and their own data.

type ArtworksResponse = ArtworksData & ErrorHandling;
type ArtistsResponse = ArtistsData & ErrorHandling;

const handleArtistsResponse = (response: ArtistsResponse) => {
  if (response.error) {
    console.error(response.error.message);
    return;
  }

  console.log(response.artists);
};

유형 스크립트의 교차로 유형

  • 타입의 컨텍스트에서 A & in TS는 교차 타입을 의미합니다.
  • 두 개체 유형의 모든 속성을 병합하고 새 유형을 생성합니다.

예:

type dog = {age: number, woof: Function};
type cat = {age: number, meow: Function};

// Type weird is an intersection of cat and dog
// it needs to have all properties of them combined
type weird = dog & cat;

const weirdAnimal: weird = {age: 2, woof: () => {'woof'}, meow: () => {'meow'}}

interface extaprop {
    color: string
}

type catDog = weird & extaprop; // type now also has added color
const weirdAnimal2: catDog = {age: 2, woof: () => {'woof'}, meow: () => {'meow'}, color: 'red'}


// This is different form a union type
// The type below means either a cat OR a dog
type dogOrCat = dog | cat;

언급URL : https://stackoverflow.com/questions/38317625/what-does-the-ampersand-mean-in-a-typescript-type-definition