러닝 타입스크립트를 읽고 요약한 내용입니다.
let poetLater: {
born: number;
name: string;
}
poetLater = {
born: 1935,
name: 'Mary Oliver',
}
poetLater = "Sappho"; // Type 'string' is not assignable to type '{ born: number; name: string; }'.
type Poet = {
born: number;
name: string;
}
let poetLater: Poet;
poetLater = {
born: 1935,
name: "Sara Teasdale",
}
poetLater = "Emily Dickinson"; // Type 'string' is not assignable to type 'Poet'.
type FirstAndLastNames = {
first: string;
last: string;
}
// Ok
const hasBoth: FirstAndLastNames = {
first: 'Sarojini',
last: 'Naidu',
}
// Property 'last' is missing in type '{ first: string; }'
// but required in type 'FirstAndLastNames'.
const hasOnlyOne: FirstAndLastNames = {
first: "Sappho"
}
type Poet = {
born: number;
name: string;
}
const poetMatch: Poet = {
born: 1928,
name: "Maya Angelou";
};
const extraProperty: Poet = {
activity: "walking",
// Object literal may only specify known properties, and 'activity' does not exist in type 'Poet'.
born: 1935,
name: "Mary Oliver"
}
type Poet = {
born: number;
name: string;
}
const existingObject = {
activity: "walking",
born: 1935,
name: "Mary Oliver",
}
const extraPropertyButOk: Poet = existingObject;
?
를 추가하면 선택적 속성임을 나타낼 수 있습니다. 유니언 타입과의 차이점이라면, 유니언 타입은 둘 중에 어느 하나의 타입은 반드시 있어야 하지만 선택적 속성은 해당 속성이 존재하지 않아도 됩니다.type Book = {
author?: string;
pages: number;
}
const ok: Book = {
author: "Rita Dove",
pages: 80,
}
const missing: Book = {
pages: 100,
}
const poem = Math.random() > 0.5
? { name: "The Double Image", pages: 7 }
: { name: "Her Kind", rhymes: true };
// 타입:
// {
// name: string;
// pages: number;
// rhymes?: undefined;
// }
// |
// {
// name: string;
// pages?: undefined;
// rhymes: boolean;
// }
type PoemWithPages = {
name: string;
pages: number;
}
type PoemWithRhymes = {
name: string;
rhymes: boolean;
}
type Poem = PoemWithPages | PoemWithRhymes;
const poem: Poem = Math.random() > 0.5
? { name: "The Double Image", pages: 7 }
: { name: "Her Kind", rhymes: true };
poem.name;
poem.pages;
// Property 'pages' does not exist on type 'Poem'.
// Property 'pages' does not exist on type 'PoemWithRhymes'.
type PoemWithPages = {
name: string;
pages: number;
}
type PoemWithRhymes = {
name: string;
rhymes: boolean;
}
type Poem = PoemWithPages | PoemWithRhymes;
const poem: Poem = Math.random() > 0.5
? { name: "The Double Image", pages: 7 }
: { name: "Her Kind", rhymes: true };
poem.name;
if("pages" in poem) {
poem.pages;
}
interface Bird {
type: "bird";
flyingSpeed: number;
}
interface Horse {
type: "horse";
runningSpeed: number;
}
type Animal = Bird | Horse;
function getAnimalSpped(animal: Animal) {
let speed: number = 0;
switch (animal.type) {
case "bird":
speed = animal.flyingSpeed;
break;
case "horse":
speed = animal.runningSpeed;
break;
}
console.log(`${animal.type} is moving with speed ${speed}km`);
}
getAnimalSpped({
type: "bird",
flyingSpeed: 10,
});
|
연산자가 &
연산자에 대응하는 역할을 하는 것처럼, 타입스크립트에서도 &
교체 타입(intersection type)을 사용해 여러 타입을 동시에 나타냅니다.type Artwork = {
genre: string;
name: string;
};
type Writing = {
pages: number;
name: string;
}
type WrittenArt = Artwork & Writing;
// 다음과 같음
// {
// genre: string;
// name: string;
// pages: number;
// }
never
- 원시 타입의 값은 동시에 여러 타입이 될 수 없기 때문에 교차 타입의 구성 요소로 함께 결합할 수 없습니다. 두 개의 원시 타입을 함께 시도하면 never
키워드로 표시되는 never
타입이 됩니다.type NotPossible = number & string; // 타입: never
never
키워드와 never
타입은 프로그래밍 언어에서 bottom 타입 또는 empty 타입을 뜻합니다. bottom 타입은 값을 가질 수 없고 참조할 수 없는 타입이므로 bottom 타입에 그 어떠한 타입도 제공할 수 없습니다.