[TypeScript] Type Alias

JoahΒ·2022λ…„ 10μ›” 5일
0

TypeScript

λͺ©λ‘ 보기
11/16

πŸ• Type Alias

μƒˆλ‘œμš΄ νƒ€μž…μ„ μ‚¬μš©μžκ°€ μ •μ˜ν•  수 μžˆλ‹€.

  type Name = string;

  const yourName: Name = "Joanna";

  console.log(yourName);
  //Joanna

alias νƒ€μž…μ„ 지정할 λ•ŒλŠ” typeμ΄λΌλŠ” ν‚€μ›Œλ“œλ₯Ό μ•žμ— μž‘μ„±ν•˜κ³  Type의 이름은 λŒ€λ¬Έμžλ‘œ μ‹œμž‘ν•œλ‹€.

μ›μ‹œν˜• νƒ€μž…λΏλ§Œ μ•„λ‹ˆλΌ μ°Έμ‘°ν˜•μΈ 객체도 κ°€λŠ₯ν•˜λ‹€.

  type Student = {
    name: string;
    age: number;
  };

  const student: Student = {
    name: "Joanna",
    age: 12
  }

Student type으둜 μ§€μ •λœ λ³€μˆ˜λŠ” λ°˜λ“œμ‹œ ν‚€ 값이 동일해야 ν•œλ‹€.

const student: Student = {
	lastName: "Joanna",  //error
  	years: 12. //error
}

ν‚€ 값은 nameκ³Ό age만 μž‘μ„±ν•  수 μžˆλ‹€.

profile
Front-end Developer

0개의 λŒ“κΈ€