λ€μ μ½λ :
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
μ κ³Ό κ΅μ°¨ν©λλ€.맀ν λ κ°μ²΄ μ νμ΄ λ¬΄μμΈμ§ μ무λ λͺ¨λ₯΄κΈ° λλ¬Έμ λΉ λ₯΄κ² μμ ν΄ λ³΄κ² μ΅λλ€.
+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};
λ¨μ΄μμ΄ λ°λ§ :
https://media1.tenor.com/images/23d9d746fc87b3a93298af43dae21f6a/tenor.gif
:)
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)
μλ‘ μ€ν¬λ‘€νμ¬ ν΄κ²° λ°©λ²μ μ°Ύμ μ μμ§λ§μ΄ λ¬Έμ κ° μ½κ° λ€λ₯Έ μλ리μ€μμ μΌμ μμ μμ μμ£Ό λ°μνλ€λ νΌλλ°±μ μ 곡νκ³ μΆμμ΅λλ€.
λ€μμ μμ λλ€.
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};
λ§€μ° μ μ©ν. κ°μ¬! π
κ°μ₯ μ μ©ν λκΈ
λ€μκ³Ό κ°μ΄ ν μ μμ΅λλ€.
Bar
μλ μμΈ μλͺ μ΄ μμ§λ§ (μ¦,(obj as Bar)[value as Foo]
λ₯Ό μν ν μ μμ΅λλ€).νΈμ§ :μ£Όμ μ¬νμ΄ λ¬Έμ κ°λμ§ μλλ‘ ν μ μλ€λ©΄ μμν κ°μ¬νκ² μ΅λλ€!