-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
147 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
|
||
### readonly, 인덱스드 스그니처, 맵드 타입 | ||
|
||
```ts | ||
interface A { | ||
readonly a:string; | ||
b: string | ||
} | ||
|
||
const q: A = { a: 'hello', b: 'world' }; | ||
q.a = 'ccc'; /// (x) | ||
``` | ||
|
||
- 인덱스드 시그니처 | ||
- 어떤키가 들어올지 모를때나 키의 갯수가 너무 많지만 모두 같은타입으로 하고싶을때 이용한다. | ||
|
||
```ts | ||
type A = { [key: string]: number }; | ||
const a: A = { a: 2, b: 3, c: 4 }; | ||
``` | ||
|
||
- 맵드 타입 | ||
```ts | ||
type B = 'Human' | 'Mammal' | 'Animal'; | ||
type A = { [key in B]: number }; | ||
const a: A = { Human: 123, Mammal: 4, Animal: 1 }; | ||
``` | ||
|
||
```ts | ||
type B = 'Human' | 'Mammal' | 'Animal'; | ||
type A = { [key in B]: B }; | ||
const a: A = { Human: 'Animal', Mammal: 'Human', Animal: 'Mammal' }; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
### 기본값 타이핑 | ||
|
||
```ts | ||
const a= (b: number = 3, c: number = 5) => { | ||
return b; | ||
} | ||
``` | ||
|
||
```ts | ||
// jsx로 설정 되어있을때의 제네릭 | ||
const add = <T = unknown>(x: T, y: T) => ({ x, y }); | ||
// const add = <T extends unknown>(x: T, y: T) => ({ x, y }); | ||
// const add = <T,>(x: T, y: T) => ({ x, y }); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
### 옵셔널, 제네릭 | ||
|
||
```ts | ||
function abc(a: number, b?: number, c?: number) {} | ||
|
||
abc(1); | ||
abc(1,2); | ||
abc(1,2,3); | ||
|
||
let obj: { a: string, b?: string } = { a: 'hello', b: 'world' } | ||
obj = { a: 'hello' }; | ||
``` | ||
|
||
- 제네릭 | ||
- 함수의 선언시가 아닌 이용할때 타입이 정해지게 만드는것 | ||
|
||
```ts | ||
function add<T>(x: T, y: T): T { | ||
return x + y; | ||
} | ||
|
||
add(1, 2); | ||
add('1', '2') | ||
add(true, false) // 제네릭에서 이용된 T의 타입이 제한되지 않았기 때문에 어떤 타입이라도 이용될 수 있다. | ||
|
||
add('1', 2); // (x) | ||
add(1, '2'); // (x) | ||
``` | ||
|
||
```ts | ||
function add<T extends string>(x:T, y: T): T { | ||
return x + y; // (x) => T가 어떤 타입인지 모른다 | ||
} | ||
add(1, 2); // (x) | ||
add('1', '2') | ||
add(true, false) // (x) | ||
|
||
add('1', 2); // (x) | ||
add(1, '2'); // (x) | ||
``` | ||
|
||
```ts | ||
function add<T extends { a: string }>(x:T): T { | ||
return x; | ||
} | ||
add({ a: 'hello' }); | ||
// <T extends {...}> | ||
// <T extends any[]> | ||
// <T extends (...args: any) => any> | ||
// <T extedns abstract new (...args: any) => any> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
### 클래스 | ||
|
||
```ts | ||
class A { | ||
private a: string; | ||
b: number; | ||
#c: number; // private | ||
constructor(a: string, b:number = 123) { | ||
this.a = '123'; | ||
this.b = 123; | ||
} | ||
|
||
method() { | ||
console.log(this.a, this.#c); | ||
} | ||
} | ||
|
||
const a = new A('123'); | ||
|
||
type Q = A; | ||
const a: A = new A('123'); | ||
``` | ||
|
||
```ts | ||
interface A { | ||
readonly a: string; | ||
b: string; | ||
} | ||
|
||
class B implements A { | ||
private a: string = 'hello'; | ||
protected b: string = 'world'; | ||
public c: string = 'hi'; | ||
|
||
method() { | ||
console.log(this.a); | ||
console.log(this.b); | ||
} | ||
} | ||
class C extends B { | ||
method() { | ||
console.log(this.a); // (x) => private는 상속받은 곳에서는 이용하지 못한다. | ||
} | ||
} | ||
|
||
new C().a; // (x) | ||
new C().b; // (x) | ||
new C().c; | ||
``` |