Typescript: λͺ¨λ“  곡용체 멀버에 μ •μ˜λ˜μ§€ μ•Šμ€ 속성에 λŒ€ν•΄ 개체 μœ ν˜•μ˜ κ³΅μš©μ²΄μ—μ„œ 속성에 μ•‘μ„ΈμŠ€ ν•  수 μ—†μŒ

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

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κ°€ μ—†μŠ΅λ‹ˆλ‹€."라고 λΆˆν‰ν•©λ‹ˆλ‹€.

Question

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

μ±…μ—μ„œ 해결책을 μ°Ύμ•˜μŠ΅λ‹ˆλ‹€.

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λŠ” μ‚¬μš©ν•œ μœ ν˜•μ„ μ•Œ 수 μžˆμŠ΅λ‹ˆλ‹€.

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

λ¬Έμ„œλŠ” λ§ν•œλ‹€ :

λ™μΌν•œ μ½”λ“œκ°€ μž‘λ™ν•˜λ„λ‘ν•˜λ €λ©΄ μœ ν˜• μ–΄μ„€ μ…˜μ„ μ‚¬μš©ν•΄μ•Όν•©λ‹ˆλ‹€.

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의 λ™μž‘μ€ κ°œλ°œμžμ—κ²Œλ³„λ‘œ λ„μ›€μ΄λ˜μ§€ μ•ŠκΈ° λ•Œλ¬Έμ— 쒋지 μ•Šλ‹€κ³  μƒκ°ν•©λ‹ˆλ‹€.

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