document에서 아이디값에 해당하는 엘리먼트를 가져오려할 때 오류가 발생
(이유는 null 일수도 있기 때문)
값을 사용할 때 물음표를 사용
testText?.id
값을 찾을 때 느낌표를 사용
document.querySelector("#someText")!
as를 이용한 타입 선언
document.querySelector("#someText") as HTMLElement
object 타입으로 선언한 변수의 키값 선택의 오류
const example: object = { value: 1 }
console.log(example.value) // 오류 발생
인터페이스를 이용한 as 활용
interface IExam { value: number }
console.log((example as IExam).value)
인터페이스를 이용한 제네릭 활용
interface IExam { value: number }
console.log((<IExam>example).value)
해당 값의 타입을 반환
아래와 같이 사용 가능
const exString: string = "some string"
const childString: typeof exString = "haha"
아래와 같이 활용 가능
- ISchool이 ICity를 상속받는 것이 참이기 때문에 number 타입으로 설정됨
interface ICity {
name: string
}
interface ISchool extends ICity{
year: number
}
type ConditionType = ISchool extends ICity ? number : string;
const text: ConditionType = 123
불러올 함수 직접 선언
import { add, minus } from "./module1"
console.log(add(1, 2), minus(3, 1))
해당 파일의 전체 함수를 불러와 별명 짓기
import * as MATH from "./module1"
console.log(MATH.add(1, 2), MATH.minus(3, 1))
네임스페이스를 이용한 모듈 선언 시 다른 패키지의 함수와 헷갈리지 않아 오류가 발생하지 않음
export namespace DANBIMATH {
export function add(a: number, b: number){
return a + b
}
// 사용시
import * as M from "./module1"
console.log(M.DANBIMATH.add(1, 2))