Typescript(기초) - 타입

런던행·2020년 6월 18일
0

TypeScript(기초)

목록 보기
1/4

// any는 모든 값의 집합, 모든 것을 할 수 있다. 되도록 사용하지 말자.

let a: any = 666 // any
let b: any = ['danger'] //any

let c = a + b // any
console.log(c)

// unknown는 any처럼 모든 값을 대표하지만 타입을 검사해 정제(refine) 하기 전까지는 타입스크립트가 unknow 타입의 값을 사용 할수 없게 강제한다. 지원하는 연산자는 비교연산, typeof, instanceof

let a1: unknown = 30 // unknown
let b1 = (a1 === 123) // boolean
//let c1 = a1 + 10 // 에러 발생

if (typeof a1 === 'number') { // 타입 검사를 꼭 해야 그 이후로 사용 할 수 있다.
let d1 = a1 + 10 // number
}

// boolean
let a2 = true // 추론
let b2 = false // 추론
const c2 = true // 추론
let d2: boolean = true // 명시
let e2: true = true // 명시
//let f2: true = false // 명시, 'false'타입을 'true'타입에 담을 수 없음

// 타입 리터럴(type literal)
// 오직 하나의 값을 나타내는 타입 -> let e2: true = true
// e2는 boolean true 타입이다.

// number 타입은 모든 숫자의 집합(.. Infinity, NaN)

let a3 = 1234 // number
let b3 = Infinity * 0.10 // number
const c3 = 5678 // 5678
let d3 = a3 < b3 // boolean
let e3: number = 100 // number
let f3: 26.218 = 26.218 // 26.218
// let g3: 26.218 = 10 // '10'타입을 '26.218'타입에 할당할 수 없음

// 긴 숫자를 처리할 때는 숫자 분리자를 이용할 수 있다.
let longNumber = 1_000_000

// bigint 타입스크립트에 새로 추가된 타입 이를 이용하면 라운딩 관련 에러 걱정없이 큰 정수를 처리할 수 있다. number는 253까지의 정수를 표현 할 수 있지만 bigint를 이용하면 이보다 큰 수도 표현할 수 있다.

let a4 = BigInt(1234) // bigint
const b4 = BigInt(5678) // 5678n

var c4 = a4 + b4 // bigint
let d4 = a4 < 1234 // boolean
// let e4 = 88.5n // error 무조건 정수를
let f4: bigint = BigInt(100) // bigint
// let g4: 100n = BigInt(100) // 100n
//let h4: bigint = 100 // error '100'타입은 'bigint'타입에 할당 할 수 없다.

// string
let a5 = 'hello' // string
var b5 = 'billy' // string
const c5 = '!'

let d5 = a5 + ' ' + b5 + c // string

let e5: string = 'zoom'
let f5: 'john' = 'john'
//let g5: 'jonh' = 'zoe' // 에러

// symbol 십벌은 es2015에 새로 추가된 기능이다.
// 객체와 맵에서 문자열 키를 대신하는 용도로 사용 심벌키를 사용하면 사람들이 잘 알려진 키만 사용하도록 강제 할 수 있음

let a6 = Symbol('a') // symbol
let b6: symbol = Symbol('b')
var c6 = a6 == b6

// let d6 = a6 + 'x'

// 객체

let a7: object = {
bb: 'x'
}

let a8: {b8: number} = {
b8: 12
}

class Person {
constructor(
public firstName: string, // public은 this.firstName = firstName을 단축시킨것
public lastName: string
) {}
}

let c8 = new Person('aaaa', 'bbbb')

let a9: {
b: number
c?: string // 포함할 수도 있다
[key: number]: boolean // boolean 타입의 값을 갖는 number 타입의 프로퍼티를 여러개를 포함 할 수 있다.
}

a9 = {b: 1}
a9 = {b: 1, c: undefined}
a9 = {b: 1, c: 'd'}
a9 = {b: 1, 10: true}

// 타입 스크립트 객체를 정의하는 방법 4가지
// 1. 객체 리터럴 {a: string}
// 2. 빈 객체 리터럴 표기법 {} -> 사용하지 말라
// 3. object타입, 어떤 필드를 가지고 있는지는 관심없고 그저 객체가 필요할 때 사용
// 4. Object타입, 사용하지 말아라

// 타입 별칭
type Age = number
type Person2 = {
name: string
age: Age
}

let age: Age = 55
let driver: Person2 = {
name: 'SSS',
age: age
}

type Cat = {name: string, purrs: boolean}
type Dog = {name: string, barks: boolean, wags: boolean}

type CatOrDogOrBoth = Cat | Dog // Cat Or Dog or 모두 포함

type CatAndDog = Cat & Dog

let a10: CatOrDogOrBoth = {
name: 'aaa',
wags: false,
barks: true
}

let b10: CatAndDog = {
name: 'aa',
barks: true,
purrs: false,
wags: true
}

// 배열
let a11 = [1, 2, 3,] // number[]
let b11 = ['a', 'b'] // string[]

let c11: string[] = ['a'] // string[]
let d11 = [1, 'a'] // (string | number)[]

let f11 = ['red'] // string[]
f11.push('blue')
// f11.push(true) error

let g11 = [] // any[]
g11.push(1) // number[]
g11.push('red') // (string|number)[]

let h11: number[] = [] // number[]
h11.push(1) // number[]
// h11.push('red') // error

// 튜플
let a12: [number] = [1]
let b12: [string, string, number] = ['a', 'b', 1]

// 일기전용 배열과 튜플
let as: readonly number[] = [1, 2, 3] // readonly number[]
let bs: readonly number[] = as.concat(4) // readonly number[]

type A = readonly string[] // readonly string[]
type B = ReadonlyArray // readonly string[]
type C = Readonly<string[]> // readonly string[]
type DD = readonly [number, string] // readonly [number, string]
type E = Readonly<[number, string]> // readonly [number, string]

// null, defined, void, never
// 1. null : 값이 없음
// 2. undefined : 아직 값을 변수에 할당하지 않음
// 3. Void : return문을 포함하지 않는 함수
// 4. never : 절대 반환하지 않는 함수

// 열거형

enum Language {
Engalish = 0,
Spanish = 1,
Russian = 2,
}

let en = Language.

profile
unit test, tdd, bdd, laravel, django, android native, vuejs, react, embedded linux, typescript

0개의 댓글