뉴히의 개발 로그
[TIL]20230801 - typescript generics 타입선언, type/interface 본문
제네릭 타입 선언
type SuperPrint = <T>(a: T[]) => T
const superPrint : SuperPrint = (a) => a[0]
const a = superPrint([1, 2, 3, 4])
const b = superPrint([true, false, true])
const c = superPrint(["a", "b", "c"])
const d = superPrint([1, 2, true, false, "hello"])
function superPrint<T>(a: T[]){
return a[0]
}
const a = superPrint<number>([1, 2, 3, 4]) //<number> 안써주면 타입스크립트가 알아서 유추함
const b = superPrint([true, false, true])
const c = superPrint(["a", "b", "c"])
const d = superPrint([1, 2, true, false, "hello"])
type Player<E> = {
name: string
extraInfo:E
}
const heejung: player<{favFood:string}> = {
name:"heejung",
extraInfo : {
favFood:"chicken"
}
}
============================== 똑같은, 이렇게 쓸수도있음
type Player<E> = {
name: string
extraInfo:E
}
type HjPlaner = player<{favFood:string}>
const heejung: HjPlaner = {
name:"heejung",
extraInfo : {
favFood:"chicken"
}
}
============================== 똑같은, 이렇게 쓸수도있음
type Player<E> = {
name: string
extraInfo:E
}
type HjExtra = {
favFood:string
}
type HjPlaner = player<HjExtra>
const heejung: HjPlaner = {
name:"heejung",
extraInfo : {
favFood:"chicken"
}
}
==================== 제네릭 재사용
const lynn: Player<null> = {
name: "lynn",
extraInfo: null
}
배열 타입 생성할때 Array<> 를 사용해서도 쓸수있음
type A = Array<number> // type A = number[] 와 같음
let a:A = [1,2,3,4]
객체지향 프로그래밍
클래스
class Player {
constructor(
private firstName: string,
private lastName: string,
public nickName:string
){}
}
const newheee = new Player("new", "heee", "뉴히");
추상클래스는 인스턴스를 생성할 수 없음! (new 사용해서 만들 수 없다)
다른 클래스가 상속만 받을 수 있음
abstract class User {
constructor(
private firstName: string,
private lastName: string,
public nickName:string
){}
}
class Player extends User {
}
const newheee = new User("new", "heee", "뉴히"); // 에러남!!!!!!!!!!!!!!!!
abstract class User {
constructor(
private firstName: string,
private lastName: string,
public nickName:string
){}
getFullName() { // 추상클래스 안에서 메소드 구현시 상속받은 클래스들은 getFullName() 호출 가능
return `${this.firstName} ${this.lastName}`
}
}
class Player extends User {
}
const newheee = new Player("new", "heee", "뉴히");
newheee.getFullName() // Player는 User로 부터 상속 받았으니 getFullName() 접근가능
추상클래스 상속 (protected 접근가능)
abstract class User {
constructor(
private firstName: string,
private lastName: string,
protected nickName:string
){}
abstract getNickName(): void
getFullNAme() {
return `${this.firstName} ${this.lastName}`
}
}
class Player extends User {
getNickName(){
console.log(this.nickName) // 상속받으면 접근 가능
}
}
const newheee = new Player("new", "heee", "뉴히");
newheee.nickNAme // 클래스 밖에선 접근 불가
[key:string]
object의 type을 선언해야 할때 사용
property의 이름은 모르고 타입만 알고 있을때 사용!
type Words = {
[key:string]: string // [key:string] 제한된 양의 property 혹은 key를 가지는 타입을 정의해 주는 방법(이름상관없음)
}
type과 interface 는 코드 사용이나 이런부분은 거의 같은데
interface는 오직 객체 생성을 위한 타입 정의만 가능하다.
type Words = string
type Numbers = number[]
interface hello = string // 안됨!!!!!!!!!!!
interface Player {
nickName:string,
words:Words,
numbers: Numbers
}