TypeScript λ²μ : 2.1.1
μνΈ
type A = { a: string; }
type B = { b: string; }
type AorB = A | B;
declare const AorB: AorB;
if (AorB.a) {
// use AorB.a
}
μμλλ λμ :
μ½λλ μ€λ₯μμ΄ μ»΄νμΌλ©λλ€. a
κ° λͺ¨λ μ‘°ν©μ κ΅¬μ± μμμ (νμ°μ μΌλ‘) μ‘΄μ¬νμ§λ μμ§λ§ μΌλΆμ μ‘΄μ¬νλ€λ μ¬μ€μ λ΄κ° .a
νκ³ μ‘΄μ¬νλ κ²½μ° μ¬μ©ν μ μλλ‘ν©λλ€.
μ€μ νλ :
Typescriptκ° "AorB μ νμ μμ± aκ° μ‘΄μ¬νμ§ μμ΅λλ€. μ ν Bμ μμ± aκ° μμ΅λλ€."λΌκ³ λΆνν©λλ€.
λ¬Έμλ λ§νλ€ :
λμΌν μ½λκ° μλνλλ‘νλ €λ©΄ μ ν μ΄μ€ μ μ μ¬μ©ν΄μΌν©λλ€.
let pet = getSmallPet();
if ((<Fish>pet).swim) {
(<Fish>pet).swim();
}
else {
(<Bird>pet).fly();
}
http://www.typescriptlang.org/docs/handbook/advanced-types.html
κ·Έλ° λ€μ μνμμ :
if ((<A>AorB).a) {
// use AorB.a
}
μ¬κΈ°μ λ¬Έμ λ B
κ° a
μμ±μ μ μΈνμ§ μκΈ° λλ¬Έμ λ°νμμ κ°λ₯ν λͺ¨λ μ νμ a
μμ±μ κ°μ§ μ μλ€λ κ²μ
λλ€ ( λ¬Έμμ΄ μ νμ b
μμ±μ΄μλ ν B
μ μμ± μ§ν©μ΄μλ κ°μ²΄). B
μμ a: undefined
μμ±μ λͺ
μ μ μΌλ‘ μ μΈνμ¬ μλνλλ‘ λ§λ€ μ μμ΅λλ€ (λ°λΌμ B
μμμ a
μμ±μ΄ μλλ‘ λ³΄μ₯).
type A = { a: string; }
type B = { b: string; a: undefined }
type AorB = A | B;
declare const AorB: AorB;
if (AorB.a) {
// Ok
}
μλ²½ν©λλ€. λ λ°©κ·.
μ ν Bμ a: undefined
λ₯Ό μ μνλ©΄ λ³μλ₯Ό μμ± / μ λ¬ν λ undefined
λ‘ μ€μ ν΄μΌν©λλ€.
μ΄ λ¬Έμ λ₯Ό ν΄κ²°νκΈ° μν΄ a
λ₯Ό a?: undefined
λ‘ μ μΈ ν μ μμΌλ©° μλ΅νλ©΄ typescriptκ° λ§μ‘± μ€λ¬μΈ κ²μ
λλ€.
λκΈμ μ¬λ°λ₯΄κ² μ΄ν΄νλ©΄ μλν©λλ€ (νμ§λ§ λμ§, νλ μ΄ κ·ΈλΌμ΄λμμ ν μ€νΈ λ¨). λ²κ·ΈμΈμ§ μλμ§? π€
type LinkProps = {
to: string;
onClick?: undefined;
// Link specific props:
target: string;
}
type ButtonProps = {
to?: undefined;
onClick: Function;
// Button specific props:
disabled: boolean;
}
type ActionProps = LinkProps | ButtonProps;
const Action = (props: ActionProps) =>
props.to ?
'Link with target: ' + props.target // Error not on ButtonProps
:
'Button with disabled: ' + props.disabled; // Error: not on LinkProps
Action({
to: 'dssd',
target: '_blank'
});
λλ μ΄κ²μ μ ν μ΄ν΄νμ§ λͺ»νλ€. if ((<A>AorB).a)
μ A
λ€μ μ
λ ₯νλλ‘ κ°μ νκΈ° λλ¬Έμ if (A.a)
λ₯Ό μ¬μ©νλ κ²κ³Ό λμΌνμ§ μμ΅λκΉ?
μ ν Bμ
a: undefined
λ₯Ό μ μνλ©΄ λ³μλ₯Ό μμ± / μ λ¬ν λundefined
λ‘ μ€μ ν΄μΌν©λλ€.
μ΄ λ¬Έμ λ₯Ό ν΄κ²°νκΈ° μν΄a
λ₯Όa?: undefined
λ‘ μ μΈ ν μ μμΌλ©° μλ΅νλ©΄ typescriptκ° λ§μ‘± μ€λ¬μΈ κ²μ λλ€.
a?: never
λ μ’μ μ μμ΅λλ€. μ΄μ μ€μλ‘ undefined
λ₯Ό ν λΉ ν μ μμ΅λλ€.
λ Έμ‘°μ κ° κ΅¬μ±μμκ² μ΄λ¦μμ£Όμ§ μμΌλ©΄ μ΄λ»κ²λ©λκΉ? (μ΄μ€ μ λ μ νμ μ΄λ¦ μμ΄λ μμ κ°μ΄ μ ν μ΄μ€ μ μ λ§λ€ μ μμ΅λκΉ?)
type u = "str" | {prop:"val"};
function f(arg:u){return arg.prop} // TypeScript: Property 'prop' does not exist on type 'u'
μ΄λ€ ν΄κ²°μ± μ΄ μμ΅λκΉ?
μ± μμ ν΄κ²°μ± μ μ°Ύμμ΅λλ€.
interface A { x: number;}
interface B { y: string;}
function doStuff ( q: A | B ) {
if ( 'x' in q) {
// if type A...
}
else {
// if type B...
}
}
λ°μ· : Basarat Ali Syed. "TypeScript μ¬μΈ΅ λΆμ." Apple Books.
κ°μ²΄μ μμ±μ΄λ©° μ ν κ°λλ‘ μ¬μ©ν μ μμΌλ©° TypeScriptλ μ¬μ©ν μ νμ μ μ μμ΅λλ€.
type ColorItemType = {
colorId: number,
colorName: string,
}
type NumItemType = {
numId: number,
numName: string
}
type ResType = {
itemId: number,
// 0 color 1 num
type: number,
itemInfo: {
colorList: Array<ColorItemType>
numList: Array<NumItemType>
}
}
const request = () => {
return [{
itemId: 1,
type: 0,
itemInfo: {
colorList: [{
colorId: 1,
colorName: 'blue'
}],
numList: []
}
}];
};
const dataSource: Array<ResType> = request();
const formatData = dataSource.map(dataItem => {
const list: Array<ColorItemType | NumItemType> = dataItem.type === 1 ? dataItem.itemInfo.numList : dataItem.itemInfo.colorList;
return list.map(listItem => {
return {
// An error will be reported here
value: listItem.numId || listItem.colorId,
label: listItem.numName || listItem.colorName
};
});
});
μ¬κΈ°μ λ¬Έμ λ
B
κ°a
μμ±μ μ μΈνμ§ μκΈ° λλ¬Έμ λ°νμμ κ°λ₯ν λͺ¨λ μ νμa
μμ±μ κ°μ§ μ μλ€λ κ²μ λλ€ ( λ¬Έμμ΄ μ νμb
μμ±μ΄μλ νB
μ μμ± μ§ν©μ΄μλ κ°μ²΄).B
μμa: undefined
μμ±μ λͺ μ μ μΌλ‘ μ μΈνμ¬ μλνλλ‘ λ§λ€ μ μμ΅λλ€ (λ°λΌμB
μμμa
μμ±μ΄ μλλ‘ λ³΄μ₯).type A = { a: string; } type B = { b: string; a: undefined } type AorB = A | B; declare const AorB: AorB; if (AorB.a) { // Ok }
λλ₯Ό μν΄ μλνμ§ μμ΅λλ€ 07.10.2020
κ·Έλ¦¬κ³ μ΄ TSμ λμμ κ°λ°μμκ²λ³λ‘ λμμ΄λμ§ μκΈ° λλ¬Έμ μ’μ§ μλ€κ³ μκ°ν©λλ€.
κ°μ₯ μ μ©ν λκΈ
μ± μμ ν΄κ²°μ± μ μ°Ύμμ΅λλ€.
λ°μ· : Basarat Ali Syed. "TypeScript μ¬μΈ΅ λΆμ." Apple Books.
κ°μ²΄μ μμ±μ΄λ©° μ ν κ°λλ‘ μ¬μ©ν μ μμΌλ©° TypeScriptλ μ¬μ©ν μ νμ μ μ μμ΅λλ€.