let spring = {
flower: "Cherry Blossom",
month: 4
};
spring['flower']; // type: string
spring.month; // type: number
spring.end;
// Error : Property 'end' does not exist on type '{ flower: string; month: number; }`.
let myState: {
name: string;
age: number;
};
// ok
myState = {
name: "hazel",
age: 22,
};
let MyState: {
name: string;
age: number;
};
let myState: MyState;
myState = {
name: "hazel",
age: 22,
};
type FirstNmae = { firstName: string };
type LastName = { lastName: string };
const fullName = {
firstName: "Lee",
lastName: "hazel",
};
// firstName
let firstName: FirstName = fullName;
// lastName
let lastName: LastName = fullName;
구조적 타이핑은 덕 타이핑과는 다르다.
1) 객체 타입으로 값을 해당 객체 타입에 할당 할 수 있는지 확인
2) 할당하는 값에는 객체 타입의 필수 속성이 있어야 한다.
type FirstAndLastNames = {
first: string;
last: string;
};
const hasBoth: FirstAndLastNames = {
first: "Lee",
last: "hazel",
};
const hasOnlyOne: FirstAndLastNames = {
// Error : Property 'last' is missing in type '{ first: string }'
// but required in type 'FirstAndNames'.
first: "Lee"
};
type Person = {
born: number;
name: string;
}
// ok: Person의 필드와 일치함
const person1: Person = {
born: 2007,
name: "Anna"
};
type Author = {
firtsName: string;
lastName: string;
};
type Poem = {
author: Author;
name: string;
};
const poemMismatch: Poem = {
author: {
name: "hazel",
//Error: Type { name : string } is not assignable to type 'Author'.
// Object literal may only specify known properties.
// and 'name' does not exist in type 'Author'.
},
name: "Tulips",
};
type Writers = {
author: string | undefined;
editor?: string;
};
// ok : author 는 undefined으로 제공
const hasRequired: Writers = {
author: undefined,
},
const missingRequried: Writers = {};
// ERROR : Property 'author' is missing in type {} but required in type 'Writers'.
const bts = Math.random() > 0.5
? { name: "taetae", age:29}
: { name: "RM", gender: true};
// type:
{
name: string;
age: number;
gender?: undefined;
} | {
name: string;
age: undefined;
gender: boolean;
}
bts.name; //string
bts.agel; // number | undefined
bts.gender; // boolean | undefined
type Person1 = {
name: string;
age: number;
};
type Person2 = {
name: string;
gender: boolean;
};
let pernson: Person1 | Person2;
const personL Person = Math.random() > 0.5
? { name: "taetae", age: 29}
: { name: "RM", gender: true};
person.name;
person.age; // Error : Property 'age' does not exist on type "Person1 | Person 2'.
person.gender;
// Error : Property 'gender' does not exist on type "Person1 | Person 2'.
if('age' in person) {
person.age;
} else {
person.gender;
}
type Person1 = {
name: string'
age: number;
};
type Person2 = {
name: string;
gender: boolean;
};
type Persons = Person1 & Person2;
//
{
person.name;
person.age;
person.gendr;
}
type ShortPoemBase = { author: string};
type Haiku = ShortPoemBase & { kigo: string; type: "haiku" };
type villanelle = ShortPoemBase & { meter: number; type: "villanelle" };
type ShortPoem = Haiku | Villanelle;
const oneArt: ShortPoem = {
// Error : type ' { author: string; type: "villanlle"; }'
// is not assignable to type 'ShortPoem'
// type '{author: string; type: "villanlle";}' is not assignable to type 'villanelle'.
// Property 'meter' is missing in type '{ author: string; type: "villanelle"}
// but required in type '{meter: numberl type: 'villanelle'}
author: "Elizabeth Bishop",
type": 'villanelle',
};
1-1 never
type MySpring = number & string; // type: never
[Reference]
1. Learning typeScript(조시 골드버그/고승원/한빛미디어_2023)