#2 Overview of TypeScript
#2.0 How Typescript Works
TS ๋ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด.
Strongly Typed Programming Language => ์ฝ๋๋ฅผ ์ปดํ์ผํด์ ๋ค๋ฅธ ์ข ๋ฅ์ ์ฝ๋๋ก ๋ณํํ๋ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด TS => JS
TS๋ฅผ ์ปดํ์ผํ๋ ๊ณผ์ ์์ ์๋ฌ๋ฅผ ํ์ธํ๋ฉด ์ปดํ์ผ์ด ์ค์ง๋๊ณ , ์๋ฌ๋ฅผ ๋ฆฌํดํจ
=> ๋ฐํ์ ์ด์ ์ ์๋ชป๋ ๋ถ๋ถ์ ํ์ธ ํ ์ ์์
#2.1 Implicit Types vs Explicit Types
TS์ ํ์ ์์คํ - ํ์ ์ถ๋ก , ๋ช ์์ ์ ์ธ
let a = "hello" // let a : string
a = "bye"
a = 1 // error : type number is ~~
// ๋ณ์ ์ ์ธ์ string์ผ๋ก ํ์ผ๋ฉด string์ผ๋ก ๋ฎ์ ์ ์์
// JS๋ ๋ฌธ์ ์์ง๋ง TS์์๋ ๋ฌธ์ ๊ฐ ๋จ
let b : boolean = "x" // error : "x"๋ boolean์ด ์๋
// ํ์
์ ์ ์ธํ๋ ๋ฐฉ์, ๋ช
์์ ์ผ๋ก ํ์
์ ์ ์ธํจ
// ๋ช
์์ ์ ์ธ์ ์ต์ํ ํ๋๊ฒ ์ข์ => ํ์
์ถ๋ก ์ ํ์ฑํ
let c = [1, 2, 3] // let c : number[]
c.push("1") // error : number array์ string์ ์ถ๊ฐํ๋ฉด ํ์
์ด ์์
#2.2 Types of TS part One
๊ธฐ๋ณธ ํ์ - number / string / boolean / ...[]
Optional ์ ํ์ ๋ณ์
const player : {
name: string,
age?: number // => ? ๋ optional ์ ํ์ ๋ณ์
} = {
name: "nico",
}
if(player.age && player.age < 10) { // undefined ์ธ ๊ฒฝ์ฐ๋ ํ์ธ
}
type ์ ์ธ
// Alias ๋ณ์นญ ํ์
์ ์ธ
type Age = number;
type Player = {
name: string,
age?: Age
}
const nico: Player = {
name: "nico"
}
const nicoco : Player = {
name: "nicoco",
age: 150
}
//////////////////////////////////
type Name = string
type Age = number
type Player = {
name: Name,
age?: Age
}
// ํจ์์ ํ์
์ ์ธ => argument์ ํ์
์ค์ & return value์ ํ์
์ค์
function playerMaker(name: string) : Player { // < : Player ๋ถ๋ถ
return {
name
}
}
// ํ์ดํ ํจ์์ธ ๊ฒฝ์ฐ
const playMaker = (name: string) : Player => ({name})
const test = playerMaker("LEE")
test.age = 12
#2.3 Types of TS part Two
readonly - ์ฝ๊ธฐ ์ ์ฉ,, ์ต์ด ์ ์ธ ํ ์์ ๋ถ๊ฐ. immutability ๋ถ์ฌ => TS์์๋ ๋ณดํธ๋ฐ์ง๋ง JS์์๋ ์ ์ฉ ์๋จ
type Age = number;
type Name = string;
type Player = {
readonly name: Name // <= ์ฝ๊ธฐ ์ ์ฉ
age?: Age
}
const playerMaker = (name:string) : Player => ({name})
const nic = playerMaker("nic")
nic.age = 12
nic.name = "name" // error
const numbers : readonly number[] = [1,2,3,4]
numbers.push(1) // error
Tuple - ์ ํด์ง ๊ฐ์์ ์์์ ์์๋ฅผ ๊ฐ์ ธ์ผํ ๋ ์ฌ์ฉ, readonly ์ฌ์ฉ ๊ฐ๋ฅ
const player1: [string, number, boolean] = [] // error
const player2: [string, number, boolean] = ["nic", 1, true]
const player3: readonly [string, number, boolean] = ["nic", 1, true]
player3[0] = "aa" // error
undefined, null, any ํ์
let a : undefined = undefined // ์ ์ธX ํ ๋นO
let b : null = null // ์ ์ธO ํ ๋นใ
let c : any = any // TS์์ ๋น ์ ธ๋์ค๊ณ ์ถ์๋ ์ฌ์ฉ, ๊ฐ๋ฅํ ์ฌ์ฉํ์ง ๋ง์
#2.4 Types of TS part Three
TS์๋ง ์๋ ํ์ ๋ค
unknown - ์ด๋ค ํ์ ์ธ์ง ๋ชจ๋ฅด๋ ๋ฐ์ดํฐ
let a : unknown;
let b = a + 1; // error => a๊ฐ number์ธ์ง ๋ชจ๋ฅด๊ธฐ ๋๋ฌธ
////
if(typeof a === 'number'){ // a ์ ํ์
์ด number ์ผ ๋
let b = a + 1
}
if(typeof a === 'string'){ // a ์ ํ์
์ด string ์ผ ๋
let b = a.toUpperCase()
}
void - ์๋ฌด๊ฒ๋ return ํ์ง ์๋ ํจ์. ์ง์ ํ์ง ์์๋ ์์์ ์ ์ฉ๋จ
function hello() {console.log('x')}
const a = hello()
a.toUpperCase() // error toUpperCase ๊ฐ ์์
never ใด ํจ์๊ฐ ์ ๋ returnํ์ง ์์๋ - ํจ์์ exception์ด ๋ฐ์ํ ๋
function hello(): never {
throw new Error("xxx")
}
function bye(name: string|number){
if(typeof name === "string"){
name // string
}else if (typeof name === "number"){
name // number
}else{
name // never
}
}
Last updated
Was this helpful?