TypeScript Basic - ChoDragon9/posts GitHub Wiki

https://www.typescriptlang.org/docs/handbook/basic-types.html

νƒ€μž… 지정

let name1 = 'John Smith';
let name2 : string = 'John Smith';
let salary : number;
let name : string = 'Alex';
let isValid : boolean;
let customerName : string = null;

λ³€μˆ˜λ‚˜ ν•¨μˆ˜μ˜ μΈμžμ— νƒ€μž…μ„ λͺ…μ‹œν•˜μ§€ μ•ŠμœΌλ©΄ TypeScript μ»΄νŒŒμΌλŸ¬λŠ” 이 λ³€μˆ˜κ°€ any νƒ€μž…μ„ μ§€μ •ν•œ κ²ƒμœΌλ‘œ κ°„μ£Όν•œλ‹€.

ν•¨μˆ˜

Javascript

function calcTax (state, income, dependents) {
  if (state === 'NY') {
  ...
  } else {
  ...
  }
}

TypeScript

function calcTax (state : string, income : number, dependents : number) : number {
  if (state === 'NY') {
  ...
  } else {
  ...
  }
}

인자 κΈ°λ³Έκ°’

기본값이 μžˆλŠ” μΈμžλŠ” μΈμžλ“€ 쀑에 λ§ˆμ§€λ§‰μ— μžˆμ–΄μ•Όν•¨.

function calcTax (dependents : number, state : string = 'NY') : number {
  if (state === 'NY') {
  ...
  } else {
  ...
  }
}

μ˜΅μ…˜ 인자

function calcTax (dependents : number, state? : string = 'NY') : number {
  if (state === 'NY') {
  ...
  } else {
  ...
  }
}