β οΈ μ 리ν λ΄μ©μ μ€νλ μλͺ»λ μ λ³΄κ° μμ μ μμ΅λλ€. λκΈλ‘ μλ €μ£Όμλ©΄ κ°μ¬νκ² μ΅λλ€.
let a: number = 1
let b = 2
let c: boolean = false
let d: string = 'TypeScript'
let f = { a: 1 }
f.a = 2
f.b = 3 // λΆκ°λ₯
let f: number[] = []
h.push(1) // κ°λ₯
h.push('a') // λΆκ°λ₯
let i: 'good' : 'good' // iλΌλ λ³μλ goodλ§ κ°λ₯νλ€.
let g: any = 3 //jsμμ νλ―μ΄ μ무 κ°μ΄λ λ€μ΄κ° μ μλ€.
function add(a: number, b: number): number {
return a + b
}
console.log(add(1,3)) // 4
interface Company {
name: string
age: number
address?: string // ?λ₯Ό λΆμ΄λ©΄ μ ν μμ±μ΄ λ¨ (optionalμ΄λΌ λΆλ¦)
}
const kakao: Company = {
name: 'kakao',
age: 10,
address: 'Seoul', // μ λ£μ΄λ μλ¬κ° λμ§ μμ(?λ₯Ό λΆμ¬μ)
}
console.log(kakao) // μ μ μΆλ ₯
// μ΅λͺ
μΈν°νμ΄μ€
const person: {
name: string
age?: number
} = {
name: 'Sugyeong',
age: 100,
}
const tuple: [string, number] = ['Sugyeong', 100]
console.log(tuple) // ['Sugyeong', 100]
enum Color {
RED,
GREEN,
BLUE,
}
const color = Color.BLUE
// if(color === Color.BLUE ){
// }
enum Color {
RED = 'red',
GREEN = 'green',
BLUE = 'blue',
}
let numOrStr: number | string = 1 // number νΉμ stringμ΄ λ€μ΄κ° μ μλ€.
let numAndStr: number & string = '' // μμ νμ
μμ μ¬μ©ν μλ μλ€.
interface Name {
name: string
}
interface Age {
age: string
}
let sunhyoup: Name & Age = {
// κ°μ λ λ€ λ£μ΄μ€μΌν¨
name: 'Sugyeong',
age: 100,
}
type Person = Name & Age
let julia: Person = {
name: 'julia',
age: 100,
}
interface Post {
title: string
content: string
}
interface ResponseData {
post?: Post
message?: string
status: number
}
const response: ResponseData[] = [
{
post: {
title: 'Hello',
content: 'Hi~'
},
status: 200
},
{
message: 'Error!'
status: 500
}
]
console.log(response[0].post.title) // Hello
console.log(response[1].post?.title) // λ°μ΄ν°κ° μλ€λ©΄ μλμΌλ‘ undefinedλ₯Ό λ°ννλ€.
interface Value<T> {
value: T
}
const value: Value<number> = {
value: 1,
}
const value: Value<string> = {
value: '1',
}
function toString<T>(a: T): string {
return `${a}`
}
console.log(toString<string>('5'))
interface User {
id: number
name: string
age: number
address: string
createAt?: string
updateAt?: string
}
// λͺ¨λ νλκ° Optionalμ΄ λλ€.
const partial: Partial<User> = {}
// λͺ¨λ νλκ° Requiredκ° λλ€.
const required: Required<User> = {
id: 1
name: 'Lee'
age: 10
address: 'Seoul'
createAt: '',
updateAt: ''
}
// νΉμ νλλ§ κ³¨λΌμ μ¬μ©ν μ μλ€.
const pick: Pick<User, 'name' | 'age'> = {
name: ''
age: '10'
}
// νΉμ νλλ§ λΉΌκ³ μ¬μ©ν μ μλ€.
const omit: Omit<User, 'id' | 'createAt' | 'updateAt'> = {
name: ''
age: '10'
address: 'Seoul'
}
//
const mixed: Omit<User, 'id' | 'address' | 'age' | 'createAt' | 'updateAt'> & Pick<Partial<User>, 'address' | 'age'> = {
name: ''
}
interface Time {
hour: number
minute: number
second: number
}
interface DateTime extends Time {
year: number
month: number
day: number
}
interface OffsetDateTime extends DateTime {
offset: number
}
interface OffsetDateTime extends DateTime {
zoneId: string
}
interface TimeFormat extends Pick<Time, 'hour' | 'minute'> {
ampm: 'am' | 'pm'
}
const timeFormat: TimeFormat = {
hour: 10
minute: 30
ampm: 'am'
}
μ€κ° νλ‘μ νΈκ° λλκ³ λ°λ‘ λ€μλ λΆν° λ€μ κ°μλ£λ μΆμ μμνμλ€π₯² μ€λμ typescriptμ λν΄μ λ°°μ λλ° μμ§ μ’ λ¨Ό μκΈ°λ‘ λ€λ Έλ€.
μ΄λ°μμΌλ‘ μ μνκ³ μ¬μ©νλ κ±°μꡬλ~λΌκ³ μΈμ§μ λ ν κ² κ°λ€. μμ£Ό μ¨κ°λ©΄μ μ΅μν΄μ§λλ‘ λ Έλ ₯ν΄μΌμ§! μμμμ!!