Typescript: '인덱슀 μ„œλͺ…μ—μ„œ μœ λ‹ˆμ˜¨μ„ μ‚¬μš©ν•  수 μ—†μŠ΅λ‹ˆλ‹€. λŒ€μ‹  맀핑 된 개체 μœ ν˜•μ„ μ‚¬μš©ν•˜μ‹­μ‹œμ˜€'에 λŒ€ν•œ λΉ λ₯Έ μˆ˜μ •

에 λ§Œλ“  2018λ…„ 05μ›” 17일  Β·  37μ½”λ©˜νŠΈ  Β·  좜처: microsoft/TypeScript

λ‹€μŒ μ½”λ“œ :

type K = "foo" | "bar";

interface SomeType {
    [prop: K]: any;
}

이 였λ₯˜ λ©”μ‹œμ§€λ₯Ό μ œκ³΅ν•©λ‹ˆλ‹€.

An index signature parameter type cannot be a union type. Consider using a mapped object type instead.

맀핑 된 개체 μœ ν˜•μ΄ 무엇인지 아무도 λͺ¨λ₯΄κΈ° λ•Œλ¬Έμ— λΉ λ₯΄κ²Œ μˆ˜μ • ν•΄ λ³΄κ² μŠ΅λ‹ˆλ‹€.

  • 인덱슀 μ„œλͺ…을 맀핑 된 μœ ν˜•μœΌλ‘œ μ „ν™˜ν•©λ‹ˆλ‹€.
  • λ‹€λ₯Έ 멀버λ₯Ό ꡐ차 μœ ν˜•κ³Ό κ²°ν•©λ˜λŠ” λ³„λ„μ˜ 객체 μœ ν˜•μœΌλ‘œ μ΄λ™ν•©λ‹ˆλ‹€.
  • ν¬ν•¨ν•˜λŠ” 객체 μœ ν˜•μ΄ μΈν„°νŽ˜μ΄μŠ€ 인 경우 ν¬ν•¨ν•˜λŠ” 객체 μœ ν˜•μ„ μœ ν˜• λ³„μΉ­μœΌλ‘œ λ³€κ²½ν•©λ‹ˆλ‹€.
  • ν¬ν•¨ν•˜λŠ” 객체 μœ ν˜•μ΄ μΈν„°νŽ˜μ΄μŠ€μ΄κ³  extends μ ˆμ΄μžˆλŠ” 경우 λͺ¨λ“  extends 절과 κ΅μ°¨ν•©λ‹ˆλ‹€.
Error Messages Quick Fixes Moderate Fixed Suggestion help wanted

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

λ‹€μŒκ³Ό 같이 ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

type Foo = 'a' | 'b';
type Bar = {[key in Foo]: any};

Bar μ—λŠ” 색인 μ„œλͺ…이 μ—†μ§€λ§Œ (즉, (obj as Bar)[value as Foo] λ₯Ό μˆ˜ν–‰ ν•  수 μ—†μŠ΅λ‹ˆλ‹€).

νŽΈμ§‘ :주의 사항이 λ¬Έμ œκ°€λ˜μ§€ μ•Šλ„λ‘ ν•  수 μžˆλ‹€λ©΄ μ˜μ›νžˆ κ°μ‚¬ν•˜κ² μŠ΅λ‹ˆλ‹€!

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

맀핑 된 개체 μœ ν˜•μ΄ 무엇인지 아무도 λͺ¨λ₯΄κΈ° λ•Œλ¬Έμ— λΉ λ₯΄κ²Œ μˆ˜μ • ν•΄ λ³΄κ² μŠ΅λ‹ˆλ‹€.

+1, 2.9κ°€ 예제 μ½”λ“œμ— 따라 인덱슀 μ„œλͺ…μœΌλ‘œ 곡용체λ₯Ό 지원할 κ²ƒμœΌλ‘œ μ˜ˆμƒν–ˆκΈ° λ•Œλ¬Έμ— 여기에 μ™”μŠ΅λ‹ˆλ‹€. λ‚˜λŠ” 이것이 μ˜€λž«λ™μ•ˆ λ°”λž¬λ˜ κΈ°λŠ₯이라고 μƒκ°ν•œλ‹€ : # 5683, # 16760 λ“±.

λ‹€μŒκ³Ό 같이 ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

type Foo = 'a' | 'b';
type Bar = {[key in Foo]: any};

Bar μ—λŠ” 색인 μ„œλͺ…이 μ—†μ§€λ§Œ (즉, (obj as Bar)[value as Foo] λ₯Ό μˆ˜ν–‰ ν•  수 μ—†μŠ΅λ‹ˆλ‹€).

νŽΈμ§‘ :주의 사항이 λ¬Έμ œκ°€λ˜μ§€ μ•Šλ„λ‘ ν•  수 μžˆλ‹€λ©΄ μ˜μ›νžˆ κ°μ‚¬ν•˜κ² μŠ΅λ‹ˆλ‹€!

λ‚˜λŠ” 이것에 λŒ€ν•΄ μΌν•˜κ³  μ‹Άλ‹€ : μ›ƒμŒ :

λ‹€λ₯Έ 멀버λ₯Ό ꡐ차 μœ ν˜•κ³Ό κ²°ν•©λ˜λŠ” λ³„λ„μ˜ 객체 μœ ν˜•μœΌλ‘œ μ΄λ™ν•©λ‹ˆλ‹€.

ν¬ν•¨ν•˜λŠ” 객체 μœ ν˜•μ΄ 클래슀 인 경우 μ–΄λ–»κ²Œν•΄μ•Όν•©λ‹ˆκΉŒ?
λ‚˜λŠ” 그것이 μΈν„°νŽ˜μ΄μŠ€λΌκ³  상상할 수 μžˆμŠ΅λ‹ˆλ‹€

κ·Έλž˜μ„œ λΉ λ₯Έ μˆ˜μ • ν›„ μ½”λ“œλ₯Ό 따라야 ν•  것은 λ¬΄μ—‡μž…λ‹ˆκΉŒ?

type K = "1" | "2"

class SomeType {
    a = 1;
    [prop: K]: any;
}

κ·Έλž˜μ„œ λΉ λ₯Έ μˆ˜μ • ν›„ μ½”λ“œλ₯Ό 따라야 ν•  것은 λ¬΄μ—‡μž…λ‹ˆκΉŒ?

λ‚˜λŠ” 이것이 κ³ μΉ  수 μ—†μ–΄μ•Όν•œλ‹€κ³  λ§ν•˜κ³  μ‹Άλ‹€ ..

@mhegazy 3.0.0-rcλ₯Ό μ‚¬μš©ν•˜κ³  있으며 μ›λž˜ κ²Œμ‹œ 된 것과 λ™μΌν•œ 였λ₯˜κ°€ 계속 λ°œμƒν•©λ‹ˆλ‹€. 이것이 μ˜ˆμƒ λ˜λŠ”κ°€?

3.0.0-rcλ₯Ό μ‚¬μš©ν•˜κ³  있으며 μ›λž˜ κ²Œμ‹œ 된 것과 λ™μΌν•œ 였λ₯˜κ°€ 계속 λ°œμƒν•©λ‹ˆλ‹€. 이것이 μ˜ˆμƒ λ˜λŠ”κ°€?

예. 였λ₯˜κ°€ μ •ν™•ν•©λ‹ˆλ‹€. 이 λ¬Έμ œλŠ” λΉ λ₯Έ μˆ˜μ •, 즉 였λ₯˜ λ©”μ‹œμ§€ μ˜†μ—μžˆλŠ” κ°€λ²Όμš΄ νŽ„ν”„λ₯Ό μΆ”κ°€ν•˜λŠ” μΆ”μ μ΄μ—ˆμŠ΅λ‹ˆλ‹€.

2.9.1 및 vscodeμ—μ„œ μ‚¬μš©ν•  μˆ˜μžˆλŠ” μ½”λ“œ μž‘μ—… μ—†μŒ

@ThaJay 이 κΈ°λŠ₯을 λ°± ν¬νŠΈν•˜μ§€ μŠ΅λ‹ˆλ‹€ . μƒˆ 버전을

λͺ…λ°±ν•˜κ²Œ. νƒ€μž„ 라인을 λ¨Όμ € ν™•μΈν•˜μ§€ μ•Šμ•„μ„œ μ£„μ†‘ν•©λ‹ˆλ‹€. 단지 μΆ©λΆ„νžˆ μƒˆλ‘œμš΄ κ²ƒμœΌλ‘œ μƒκ°ν–ˆμŠ΅λ‹ˆλ‹€. TSκ°€ μ²˜μŒμž…λ‹ˆλ‹€. 버전 3μ—μ„œ ν™•μΈν•©λ‹ˆλ‹€.

이것을 μ„€λͺ…ν•˜λŠ” 방법 :

function createRequestTypes(base){
  return ['REQUEST', 'SUCCESS', 'FAILURE'].reduce((acc, type) => {
    acc[type] = `${base}_${type}`
    return acc
  }, {})
}

const user = createRequestTypes('USER')
console.log(user.REQUEST) // error
// just string? like:
interface IRequestType: {[key: string]: string}

μ•„λž˜μ—μ„œ μ‹œλ„ν–ˆμ§€λ§Œ λͺ¨λ‘ μ‹€νŒ¨ν–ˆμŠ΅λ‹ˆλ‹€.

type requestStatus = 'REQUEST' | 'SUCCESS' | 'FAILURE'
type requestTypes = {
  [key in requestStatus]: string
}
// or
interface IRequestTypes {[key: keyType]: string}
// or even
type requestTypes = {
  FAILURE: string,
  SUCCESS: string,
  REQUEST: string
}

@maicWorkGithub μ—¬κΈ° μžˆμŠ΅λ‹ˆλ‹€.

const user = createRequestTypes('USER')
console.log(user.REQUEST) 

function createRequestTypes(base:string):requestTypes {
  const result : requestTypes    = {}
  const arr    : requestStatus[] = ['REQUEST', 'SUCCESS', 'FAILURE']  

  return arr.reduce((acc, type) => {
    acc[type] = `${base}_${type}`
    return acc
  }, result)
}


type requestStatus = 'REQUEST' | 'SUCCESS' | 'FAILURE'
type requestTypes = { [key in requestStatus]?: string }

@ihorskyi κ°μ‚¬ν•©λ‹ˆλ‹€ !!

type μž‘λ™ν•˜μ§€λ§Œ interface κ°€ μž‘λ™ν•˜μ§€ μ•ŠλŠ” μ΄μœ κ°€ κΆκΈˆν•©λ‹ˆλ‹€. λˆ„κ΅°κ°€ μ„€λͺ…ν•΄ μ£Όμ‹œκ² μŠ΅λ‹ˆκΉŒ? interface μ΄λŸ¬ν•œ μ œν•œ (λ˜λŠ” κΈ°λŠ₯)의 μ΄μœ λŠ” λ¬΄μ—‡μž…λ‹ˆκΉŒ?

type Foo = 'a' | 'b';
type Bar = {[key in Foo]: any}; // ok
interface Baz {[key in Foo]: any} // =>

// A computed property name in an interface must refer to an expression whose type is a literal type or a 'unique symbol' type.ts(1169)
// A computed property name must be of type 'string', 'number', 'symbol', or 'any'.ts(2464)
// 'Foo' only refers to a type, but is being used as a value here.ts(2693)

이것은 발견 ν•  μˆ˜μžˆλŠ” λ†€λΌμš΄ μžλ™ μˆ˜μ •μ΄μ—ˆμŠ΅λ‹ˆλ‹€. κ΅¬ν˜„ν•΄ μ£Όμ…”μ„œ κ°μ‚¬ν•©λ‹ˆλ‹€! :)

μˆ˜μ—…λ„ λ§ˆμ°¬κ°€μ§€μž…λ‹ˆλ‹€.

λ‹€μŒκ³Ό 같이 ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

type Foo = 'a' | 'b';
type Bar = {[key in Foo]: any};

Bar μ—λŠ” 색인 μ„œλͺ…이 μ—†μ§€λ§Œ (즉, (obj as Bar)[value as Foo] λ₯Ό μˆ˜ν–‰ ν•  수 μ—†μŠ΅λ‹ˆλ‹€).

νŽΈμ§‘ :주의 사항이 λ¬Έμ œκ°€λ˜μ§€ μ•Šλ„λ‘ ν•  수 μžˆλ‹€λ©΄ μ˜μ›νžˆ κ°μ‚¬ν•˜κ² μŠ΅λ‹ˆλ‹€!

λŒ€μ‹  Record μ‚¬μš©ν•˜μ„Έμš”!

type Foo = 'a' | 'b'
type Bar = Record<Foo, any>

클래슀λ₯Ό μ‚¬μš©ν•˜μ—¬ 이에 λŒ€ν•œ 예λ₯Ό ν•˜λ‚˜ 더 μΆ”κ°€ν•˜λ €λ©΄ ...

class Foo {
   a: string;
   b: string;
}

type Bar = {[key in keyof Foo]: any};

Partial을 μ‚¬μš©ν•  λ•Œ 더 μ’‹μŠ΅λ‹ˆλ‹€.

type A = 'x' | 'y' | 'z';
type M = Partial<{
    [key in A]: boolean
}>;

맀핑 된 개체 μœ ν˜•μ΄ 무엇인지 아무도 λͺ¨λ₯΄κΈ° λ•Œλ¬Έμ— λΉ λ₯΄κ²Œ μˆ˜μ • ν•΄ λ³΄κ² μŠ΅λ‹ˆλ‹€.

μ•ˆλ…•ν•˜μ„Έμš”.
였λ₯˜ λ©”μ‹œμ§€κ°€ λŒ€λ‹΅μ„ μ œμ•ˆ ν•  μˆ˜μ—†λŠ” μ΄μœ λŠ” λ¬΄μ—‡μž…λ‹ˆκΉŒ?

μœ ν˜• λ˜λŠ” μ—΄κ±° ν˜• 을 ν‚€λ‘œ μ‚¬μš©ν•˜λŠ” μΈν„°νŽ˜μ΄μŠ€κ°€ ν•˜λ‚˜μ˜ 속성 만 ν—ˆμš© ν•  수 μžˆλ‹€κ³  말할 수 μžˆλŠ”μ§€ μ•„λŠ” μ‚¬λžŒμ΄ μžˆμŠ΅λ‹ˆκΉŒ?

예λ₯Ό λ“€μ–΄ λ‹€μŒκ³Ό 같은 μ„œλͺ…이 μžˆμŠ΅λ‹ˆλ‹€ : { <field>: { <and|or|xor>: <int> } } took from mongo bitwise operator .

export enum BitwiseOperator {
    and = "and",
    or = "or",
    xor = "xor",
}

export type BitwiseCondition = {
    [key in BitwiseOperator]?: number;
}

그런 λ‹€μŒ 그것을 μ‚¬μš©ν•  λ•Œ μΈν„°νŽ˜μ΄μŠ€μ— μ˜ν•΄ μ •μ˜ 된 λ³€μˆ˜μ— ν•˜λ‚˜μ˜ 속성 만 μžˆλŠ”μ§€ ν™•μΈν•˜κ³  μ‹ΆμŠ΅λ‹ˆλ‹€.

const query: BitwiseCondition = {
  and: 5,
  or: 6  // raise a ts error
};

@ b4dnewz Typescriptμ—μ„œλŠ” ν•  수 μ—†μŠ΅λ‹ˆλ‹€. ν•΄κ²° 방법 : https://github.com/Microsoft/TypeScript/issues/10575

@ b4dnewz , 1 개의 속성 만 μ›ν•œλ‹€λ©΄ μ΄λ ‡κ²Œν•˜μ§€ μ•ŠλŠ” μ΄μœ λŠ” λ¬΄μ—‡μž…λ‹ˆκΉŒ?

export enum BitwiseOperator {
  and = "and",
  or = "or",
  xor = "xor",
}

export type BitwiseCondition = {
  operator: BitwiseOperator;
  value: number;
}

@benwinding λΆˆν–‰νžˆλ„ λ°˜ν™˜ 된 λͺ¨μ–‘은 mongodbκ°€ κΈ°λŒ€ν•˜λŠ” 것과 λ‹€λ¦…λ‹ˆλ‹€.

@apieceofbart μ œμ•ˆ

λ‚˜λŠ” 두톡을 ν”Όν•˜κΈ° μœ„ν•΄ mongo-operators μ •μ˜λ₯Ό κ°€λŠ₯ν•œ ν•œ κ°„λ‹¨ν•˜κ²Œ μœ μ§€ν•˜λ €κ³  λ…Έλ ₯ν•˜κ³  μžˆμŠ΅λ‹ˆλ‹€ 😁 ν–₯ν›„ μ μ ˆν•œ 지원이 μΆ”κ°€ 될 수 μžˆμŠ΅λ‹ˆλ‹€

@ b4dnewz μΆ©λΆ„νžˆ κ³΅μ •ν•œ,

μ‚¬μš©ν•  μˆ˜μžˆλŠ” 더 κ°„λ‹¨ν•œ μ˜΅μ…˜μ€ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

export type BitwiseCondition =
  | { or: number }
  | { xor: number }
  | { and: number }

λ„ˆλ¬΄ λ§Žμ€ 쀑볡없이 얻을 μˆ˜μžˆλŠ” κ°€μž₯ κ°€κΉŒμš΄ κ²ƒμž…λ‹ˆλ‹€.

@ b4dnewz μΆ©λΆ„νžˆ κ³΅μ •ν•œ,

μ‚¬μš©ν•  μˆ˜μžˆλŠ” 더 κ°„λ‹¨ν•œ μ˜΅μ…˜μ€ λ‹€μŒκ³Ό κ°™μŠ΅λ‹ˆλ‹€.

export type BitwiseCondition =
  | { or: number }
  | { xor: number }
  | { and: number }

λ„ˆλ¬΄ λ§Žμ€ 쀑볡없이 얻을 μˆ˜μžˆλŠ” κ°€μž₯ κ°€κΉŒμš΄ κ²ƒμž…λ‹ˆλ‹€.

이 μ˜ˆμ—μ„œλŠ” 였λ₯˜κ°€ λ°œμƒν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

const query: BitwiseCondition = {
  and: 5,
  or: 6  // raise a ts error
};

그게 μš”μ μ΄λΌκ³  생각 ν–ˆμ–΄

@apieceofbart ,

이 μ˜ˆμ—μ„œλŠ” 였λ₯˜κ°€ λ°œμƒν•˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€.

export type BitwiseCondition =
  | { or: number }
  | { xor: number }
  | { and: number }

const query: BitwiseCondition = {
  and: 5,
  or: 6  // doesn't raise a ts error!
};

와! 이상 ν•΄μš” : open_mouth : λͺ°λžμ–΄μš”!

TypescriptλŠ” 객체에 λŒ€ν•΄ μƒν˜Έ 배타적 인 μœ ν˜•μ„ μ§€μ›ν•˜μ§€ μ•ŠλŠ” 것 κ°™μŠ΅λ‹ˆλ‹€. λ˜ν•œ μ—¬κΈ°μ—μ„œ 언어에 λŒ€ν•œ μ œμ•ˆμ΄μ—ˆμŠ΅λ‹ˆλ‹€ : https://github.com/microsoft/TypeScript/issues/14094

κ·Έλž˜λ„ κΈ°μˆ μ μœΌλ‘œλŠ” μ—¬μ „νžˆ κ°€λŠ₯ν•©λ‹ˆλ‹€ ...

이 stackoverflow λ‹΅λ³€μ—μ„œ 쑰건뢀 μœ ν˜• (κ°€μž₯ μ–΄λ €μš΄ μœ ν˜•)을 μ‚¬μš©ν•˜μ—¬ 이것을 달성 ν•  수 μžˆμ§€λ§Œ μ˜ˆμ˜μ§€ μ•ŠμŠ΅λ‹ˆλ‹€ ....

/*
 XOR boiler plate
*/
type Without<T, U> = { [P in Exclude<keyof T, keyof U>]?: never };
type XOR<T, U> = T | U extends object
  ? (Without<T, U> & U) | (Without<U, T> & T)
  : T | U;
type XOR3<S, T, U> = XOR<S, XOR<T, U>>;

// Code start
export type BitwiseCondition = XOR3<
  { or: number },
  { xor: number },
  { and: number }
>;

const query1: BitwiseCondition = {
  and: 5
};

const query: BitwiseCondition = {
  and: 5,
  or: 6 // raise a ts error
};

λˆ„κ΅°κ°€ 이걸 더 μ˜ˆμ˜κ±°λ‚˜ 더 μ’‹κ²Œ λ§Œλ“€ 수 μžˆλ‹€λ©΄

@mvasin FWIW, 이것은 λ™μΌν•œ κ²°κ³Όλ₯Ό μ–»κΈ° μœ„ν•΄ _appears_ν•˜μ§€λ§Œ μœ ν˜•μ—μ„œμ™€ λ§ˆμ°¬κ°€μ§€λ‘œ μΈν„°νŽ˜μ΄μŠ€μ˜ κΈ°λŠ₯μ΄μ–΄μ•Όν•œλ‹€λŠ” 데 μ „μ μœΌλ‘œ λ™μ˜ν•©λ‹ˆλ‹€.

type Foo = 'a' | 'b';

type Bar = {
  [key in Foo]: any
}

interface A extends Bar { }

class Wol implements A{
  a: any;
  b: any;
}

typescript 3.5의 경우 λ‹€μŒμ„ μˆ˜ν–‰ν•΄μ•Όν•˜λŠ” 것 κ°™μŠ΅λ‹ˆλ‹€.

export interface DataTableState {
  columnStats: {[key in keyof DataTable]?:{}}
}

이것이 μ΅œμ„ μ˜ λ°©λ²•μž…λ‹ˆκΉŒ?

인덱슀 μ„œλͺ…이 μ—΄κ±° ν˜• μœ ν˜•μ„ μ •ν™•νžˆ μ‚¬μš©ν•  μˆ˜μ—†λŠ” μ΄μœ λŠ” λ¬΄μ—‡μž…λ‹ˆκΉŒ? 맀핑 된 μœ ν˜•μ€ λ‚΄κ°€ μ›ν•˜λŠ”λŒ€λ‘œ 거의 μˆ˜ν–‰ν•˜μ§€λ§Œ TypeScriptλŠ” μ—΄κ±° ν˜•μ˜ λͺ¨λ“  λ¬Έμžμ—΄μ΄ μ •μ˜ 된 ν‚€λ‘œ μ‘΄μž¬ν•˜κΈ°λ₯Ό κΈ°λŒ€ν•©λ‹ˆλ‹€. μ‹€μ œλ‘œ λͺ¨λ“  ν‚€κ°€ μ‘΄μž¬ν•œλ‹€κ³  μ£Όμž₯ν•˜κ³  μ‹Άμ§€λŠ” μ•ŠμŠ΅λ‹ˆλ‹€. ν‚€κ°€ 있으면 μ—΄κ±° ν˜•μ— μžˆμ–΄μ•Όν•œλ‹€λŠ” κ²ƒμž…λ‹ˆλ‹€.

예λ₯Ό λ“€μ–΄ μœ ν˜• :

type MyType = {
  [Key: 'foo' | 'bar' | 'zip']: number;
};

λ‹€μŒμ„ μΆ©μ‘±ν•΄μ•Όν•©λ‹ˆλ‹€.

const x: MyType = {
  foo: 1,
  zip: 2
};

맀핑 된 μœ ν˜•μ— λŒ€ν•΄ μ •μ˜λ˜μ§€ μ•Šμ€ λ‹€λ₯Έ ν‚€λ₯Ό μ„€μ •ν•  수 μžˆμ§€λ§Œ ν‚€λ₯Ό μ„ νƒμ μœΌλ‘œ μ„€μ •ν•˜λŠ” 것을 μ„ ν˜Έν•˜μ§€λ§Œ ν‚€κ°€μžˆλŠ” 경우 κ°’ 을 μ •μ˜

Partial을 μ‚¬μš©ν•  λ•Œ 더 μ’‹μŠ΅λ‹ˆλ‹€.

type A = 'x' | 'y' | 'z';
type M = Partial<{
    [key in A]: boolean
}>;

감사!
사전과 λΆ€λΆ„μ μœΌλ‘œ μΌμΉ˜ν•˜λŠ” μœ ν˜•μ„ μ •μ˜ν•΄μ•Ό ν•  λ•Œ μœ μš©ν•©λ‹ˆλ‹€.

"λΆ€λΆ„"은 λ ˆμ½”λ“œμ—μ„œλ„ μ‚¬μš©ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

type Foo = 'a' | 'b';
let foo1: Record<Foo, number> = { a: 1, b: 2 };
let foo2: Partial<Record<Foo, number>> = { a: 1 };

맀월이 GitHub νŽ˜μ΄μ§€λ₯Ό λ¬΄μ˜μ‹μ μœΌλ‘œ λ°©λ¬Έν•©λ‹ˆλ‹€.

λ‚΄ 졜근의 것은 정말 κ°„λ‹¨ν•œ κ²ƒμž…λ‹ˆλ‹€.

interface ObjectLiteral {
    [key: string | number]: any
}
export const mapToObjectLiteral = (map: Map<string|number, any>) =>
    Array.from(map).reduce((objLit, [key, value]) => {
        objLit[key] = value
        return objLit
    }, {} as ObjectLiteral)

image

μœ„λ‘œ μŠ€ν¬λ‘€ν•˜μ—¬ ν•΄κ²° 방법을 찾을 수 μžˆμ§€λ§Œμ΄ λ¬Έμ œκ°€ μ•½κ°„ λ‹€λ₯Έ μ‹œλ‚˜λ¦¬μ˜€μ—μ„œ 일상 μž‘μ—…μ—μ„œ 자주 λ°œμƒν•œλ‹€λŠ” ν”Όλ“œλ°±μ„ μ œκ³΅ν•˜κ³  μ‹Άμ—ˆμŠ΅λ‹ˆλ‹€.

λ‹€μŒμ€ μ˜ˆμž…λ‹ˆλ‹€.

type MapKey = string | number;
type ObjectLiteral<T extends MapKey, V = any> = {
  [P in T extends number ? string : T]: V;
};

export const mapToObjectLiteral = <T extends MapKey, V>(map: Map<T, V>) =>
  Array.from(map).reduce((objLit, [key, value]) => {
    objLit[key as keyof ObjectLiteral<T>] = value;
    return objLit;
  }, {} as ObjectLiteral<T, V>);

// how to make a better type of map ?
const m = new Map<1 | "foo", "a" | "b">();
m.set(1, "a");
m.set("foo", "b");

const o = mapToObjectLiteral(new Map(m));

console.log(o[1], o.foo); // just got an union type of every member of 'o'

https://github.com/microsoft/TypeScript/issues/24220#issuecomment -504285702

클래슀λ₯Ό μ‚¬μš©ν•˜μ—¬ 이에 λŒ€ν•œ 예λ₯Ό ν•˜λ‚˜ 더 μΆ”κ°€ν•˜λ €λ©΄ ...

class Foo {
   a: string;
   b: string;
}

type Bar = {[key in keyof Foo]: any};

맀우 μœ μš©ν•œ. 감사! πŸš€

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