const a: {} = 'sdt'
const b: Object = 4
const c: object = '4'
const d: object = {hello: 'world'}
type A = { a: string; b: string; c: string };
type B = { [key: string]: string };
type C = "Human" | "Animal" | "Mammal";
type D = { [key in C]: number };
const test: D = { Animal: 4, Human: 1, Mammal: 10 };
class A {
a: string;
b: number;
constructor(a: string, b: number = 123) {
this.a = a;
this.b = b;
}
method() {}
}
typeof A;
const test: A = new A("asd");
interface A {
readonly a: string;
b: string;
}
class B implements A {
private a: string = "123";
protected b: string = "asdf";
c: string = "wow";
method() {
console.log(this.a);
}
}
class C extends B {
method() {
console.log(this.a);
console.log(this.b);
console.log(this.c);
}
}
new C().c;
new C().a;
new C().b;
abstract class B {
private readonly a: string = "123";
b: string = "asdf";
c: string = "wow";
abstract method(): void;
method2() {
return "3";
}
}
class C extends B {
method() {
console.log(this.b);
console.log(this.c);
}
}
- private vs protected

function abc(a: number, b?: number, c?: number) {}
abc(1);
abc(1, 2);
abc(1, 2, 3);
abc(1, 2, 3, 4);
function abcd(...arg: number[]) {}
- 제네릭 활용 예시
코드 작성 시에는 타입이 뭐가 될지 모르지만, 코드 실행하면 확실히 알게된다.
interface Array<T> {
forEach(
callbackfn: (value: T, index: number, array: T[]) => void,
thisArg?: any
): void;
}
const a: Array<number> = [1, 2, 3];
a.forEach((value) => {
console.log(value);
});
["1", "2", "3"].forEach((value) => {
console.log(value);
});
["1", 2, false].forEach((value) => {
console.log(value);
});
function add<T>(x: T, y: T): T {
return x + y;
}
add<number>(1, 2);
add("1", "2");
interface Array<T> {
map<U>(
callbackfn: (value: T, index: number, array: T[]) => U,
thisArg?: any
): U[];
}
const strs = [1, 2, 3].map((value) => value.toString());
interface Array<T> {
filter<S extends T>(
predicate: (value: T, index: number, array: T[]) => value is S,
thisArg?: any
): S[];
filter(
predicate: (value: T, index: number, array: T[]) => unknown,
thisArg?: any
): T[];
}
const predicate = (value: string | number): value is string =>
typeof value === "string";
const strs = [1, "3", 2, "5", 3].filter(predicate);
interface Arr<T> {
forEach(callbackfn: (item: T) => void): void;
forEach(
callbackfn: (value: T, index: number, array: T[]) => void,
thisArg?: any
): void;
}
const a: Arr<number> = [1, 2, 3];
a.forEach((item) => {
console.log(item);
});
a.forEach((item) => {
console.log(item);
return 3;
});
const b: Arr<string> = ["1", "2", "3"];
b.forEach((item) => {
console.log(item);
});
b.forEach((item) => {
console.log(item);
return "3";
});
interface Arr<T> {
map<S>(callbackfn: (item: T, index: number) => S): S[];
}
const a: Arr<number> = [1, 2, 3];
const b = a.map((item, index) => item + 2);
const c = a.map((item, index) => a.toString());
const d = a.map((item, index) => item % 2 === 0);
const e: Arr<string> = ["1", "2", "3"];
const f = e.map((item) => item + 2);
interface Arr<T> {
map<S>(callbackfn: (item: T, index: number) => S): S[];
filter<S extends T>(callbackfn: (item: T) => item is S): S[];
}
const a: Arr<number> = [1, 2, 3];
const b = a.filter((item): item is number => item % 2 === 0);
const c: Arr<number | string> = [1, "2", 3];
const d = c.filter((item): item is string => typeof item === "string");
const predicate = (item: string | number): item is number =>
typeof item === "number";
const e = c.filter(predicate);
- 공변성, 반공변성
함수 간에 서로 대입할 수 있냐, 없냐를 따지는 것
function a(x: string): number {
return +x;
}
a("2");
type B = (x: string) => number | string;
const b: B = a;
function a(x: string): number | string {
return +x;
}
a("2");
type B = (x: string) => number;
const b: B = a;
function a(x: number | string): number {
return +x;
}
type B = (x: string) => number;
const b: B = a;
function a(x: number | string): number {
return +x;
}
type B = (x: string) => number | string;
const b: B = a;
interface Add {
(x: number, y: number): number;
(x: string, y: string): string;
}
const add: Add = (x, y) => x + y;
class A {
add(x: number, y: number): number;
add(x: string, y: string): string;
add(x: any, y: any) {
return x + y;
}
}
const a = new A().add("1", "2");