Typescript: μ œλ„€λ¦­κ³Ό ν•¨κ»˜ typeof μ‚¬μš©

에 λ§Œλ“  2017λ…„ 12μ›” 15일  Β·  3μ½”λ©˜νŠΈ  Β·  좜처: microsoft/TypeScript

TypeScript 버전 : ν˜„μž¬ ν”Œλ ˆμ΄ κ·ΈλΌμš΄λ“œ 버전 (?)

μ•”ν˜Έ

class GenericGroup<T> {
  items: Array<T> = [];
  constructor(name: string) {}
}

type CheckboxValues = string;
type CheckboxesGroup = new (name: string) => GenericGroup<CheckboxValues>;
const Checkboxes: CheckboxesGroup = GenericGroup;
const checkboxes = new Checkboxes('checkboxes');
checkboxes.items.map(item => item.toUpperCase());


type RadioValues = string;
type RadiosGroup = typeof GenericGroup<RadioValues>;
const Radios: RadiosGroup = GenericGroup;
const radios = new Radios('radios');
radios.items.map(item => item.toUpperCase());

링크

μ˜ˆμƒλ˜λŠ” λ™μž‘ :

typeof GenericGroup<RadioValues>; λŠ” new (name: string) => GenericGroup<RadioValues>; 와 λ™μΌν•©λ‹ˆλ‹€.

μ‹€μ œ 행동 :

typeof GenericGroup<RadioValues>; 이 μ§€μ›λ˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ— ꡬ문 였λ₯˜μž…λ‹ˆλ‹€.

자극:

λ‚΄ μ‚¬μš© μ‚¬λ‘€μ—λŠ” 타사 라이브러리의 클래슀λ₯Ό _ ν™•μž₯ _ν•˜λŠ” 일반 ( GenericGroup ) ν΄λž˜μŠ€κ°€ μžˆμŠ΅λ‹ˆλ‹€. 타사 lib의 ν΄λž˜μŠ€λŠ” μƒμ„±μžμ—μ„œ μ—¬λŸ¬ 맀개 λ³€μˆ˜λ₯Ό μ‚¬μš©ν•©λ‹ˆλ‹€. μ±„μ›Œμ§„ μ œλ„€λ¦­μœΌλ‘œ 클래슀 별칭을 μ§€μ •ν•˜λ©΄ μ‹€μ œλ‘œ μœ μ§€ κ΄€λ¦¬ν•˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ— 맀번 타사 lib에 λŒ€ν•œ 맀개 λ³€μˆ˜λ₯Ό μž‘μ„±ν•˜κ³  싢지 μ•ŠμŠ΅λ‹ˆλ‹€ ( new (name: string) => GenericGroup<CheckboxValues>; ).

(κ΄€λ ¨λœ μ—¬λŸ¬ λ¬Έμ œκ°€μžˆλŠ” 것 κ°™μ§€λ§Œ μ •ν™•νžˆμ΄ λ¬Έμ œμ— λŒ€ν•œ 문제λ₯Ό 찾을 수 μ—†μŠ΅λ‹ˆλ‹€.)

Question

κ°€μž₯ μœ μš©ν•œ λŒ“κΈ€

'typeof'μ—°μ‚°μžλŠ” μœ ν˜•μ˜ μƒμ„±μžλ₯Ό μ„€λͺ…ν•˜λŠ” 것이 μ•„λ‹ˆλΌ κ°’μ˜ μœ ν˜•μ„ μ„€λͺ…ν•˜κΈ°μœ„ν•œ κ²ƒμž…λ‹ˆλ‹€. SomeGeneric값이 μ•„λ‹ˆλΌ κ·Έ 자체의 μœ ν˜•μž…λ‹ˆλ‹€. λ‹€λ₯Έ μΈν„°νŽ˜μ΄μŠ€ / μœ ν˜• (μ•„λž˜μ— μ •μ˜ 된 것과 같은)도 μ΄λŸ¬ν•œ λ°©μ‹μœΌλ‘œ μž‘λ™ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

interface MyInterface {
    name: string;
}

let m: typeof MyInterface; // error "MyInterface only refers to a type, but is being used as a value here."

이에 λŒ€ν•œ 해결책은 Angular와 같은 μƒμ„±μžλ₯Ό μ„€λͺ…ν•˜λŠ” μ œλ„€λ¦­ μœ ν˜•μ„ μ •μ˜ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€.

// angular/packages/core/src/type.ts

export interface Type<T> extends Function { 
    new (...args: any[]): T; 
}

( angular / packages / core / src / type.ts )μ—μ„œ

μ‚¬μš© 예 :

type RadioValues = string;
type RadiosGroup = Type<GenericGroup<RadioValues>>;
const Radios: RadiosGroup = GenericGroup;
const radios = new Radios('radios');
radios.items.map(item => item.toUpperCase());

λͺ¨λ“  3 λŒ“κΈ€

'typeof'μ—°μ‚°μžλŠ” μœ ν˜•μ˜ μƒμ„±μžλ₯Ό μ„€λͺ…ν•˜λŠ” 것이 μ•„λ‹ˆλΌ κ°’μ˜ μœ ν˜•μ„ μ„€λͺ…ν•˜κΈ°μœ„ν•œ κ²ƒμž…λ‹ˆλ‹€. SomeGeneric값이 μ•„λ‹ˆλΌ κ·Έ 자체의 μœ ν˜•μž…λ‹ˆλ‹€. λ‹€λ₯Έ μΈν„°νŽ˜μ΄μŠ€ / μœ ν˜• (μ•„λž˜μ— μ •μ˜ 된 것과 같은)도 μ΄λŸ¬ν•œ λ°©μ‹μœΌλ‘œ μž‘λ™ν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

interface MyInterface {
    name: string;
}

let m: typeof MyInterface; // error "MyInterface only refers to a type, but is being used as a value here."

이에 λŒ€ν•œ 해결책은 Angular와 같은 μƒμ„±μžλ₯Ό μ„€λͺ…ν•˜λŠ” μ œλ„€λ¦­ μœ ν˜•μ„ μ •μ˜ν•˜λŠ” κ²ƒμž…λ‹ˆλ‹€.

// angular/packages/core/src/type.ts

export interface Type<T> extends Function { 
    new (...args: any[]): T; 
}

( angular / packages / core / src / type.ts )μ—μ„œ

μ‚¬μš© 예 :

type RadioValues = string;
type RadiosGroup = Type<GenericGroup<RadioValues>>;
const Radios: RadiosGroup = GenericGroup;
const radios = new Radios('radios');
radios.items.map(item => item.toUpperCase());

λ―Έλ¬˜ν•œ 점은 μ œλ„€λ¦­ 클래슀 μƒμ„±μž ν•¨μˆ˜ κ°€ μ œλ„€λ¦­ μœ ν˜•μ— ν¬ν•¨λ˜μ–΄ μžˆμ§€ μ•Šλ‹€λŠ” κ²ƒμž…λ‹ˆλ‹€. μœ ν˜• 맀개 λ³€μˆ˜κ°€ 정적 λ©€λ²„μ˜ λ²”μœ„μ— μžˆμ§€ μ•ŠκΈ° λ•Œλ¬Έμ— μ•Œ 수 μžˆμŠ΅λ‹ˆλ‹€! 당신이 이런 νƒ€μž…μ„ κ°€μ§€κ³ μžˆλŠ” κ²ƒμ²˜λŸΌ "λŠλ‚Œ"

interface GenericCtor<T> {
  new(x: string): InstanceType<T>;
}

근데 μ§„μ§œ νƒ€μž…μ€ μ΄λ ‡κ²Œ

interface GenericCtor {
  new<T>(x: string): InstanceType<T>;
}

λ‘˜ λ‹€ κ°μ‚¬ν•©λ‹ˆλ‹€. λ‚˜λŠ” 이것에 λ‚΄ 머리λ₯Ό κ°μŒ€ ν•„μš”κ°€μžˆλ‹€. 제 κ²½μš°μ—λŠ” 타사 μƒμ„±μž ( React.Component )μ—μ„œ ν™•μž₯ν•˜λ―€λ‘œ 여기에 ν”Όλ“œλ°±μ„ μ μš©ν•˜λŠ” 방법을 μ•Œμ•„ λ‚΄μ•Όν•©λ‹ˆλ‹€. 감사.

이 νŽ˜μ΄μ§€κ°€ 도움이 λ˜μ—ˆλ‚˜μš”?
0 / 5 - 0 λ“±κΈ‰