Typescript: nullable์ด ์•„๋‹Œ ์œ ํ˜•์€ ์„ ํƒ์  ๋””์ŠคํŠธ ๋Ÿญ์ฒ˜๋ง (ng2)์œผ๋กœ ์ž˜ ์ž‘๋™ํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

์— ๋งŒ๋“  2016๋…„ 05์›” 19์ผ  ยท  1๋…ผํ‰  ยท  ์ถœ์ฒ˜: microsoft/TypeScript

TypeScript ๋ฒ„์ „ :

์•ผ๊ฐ„ (1.9.0-dev.20160217)

์•”ํ˜ธ

export interface QueryMetadataFactory {
    (selector: Type | string, {descendants, read}?: {
        descendants?: boolean;
        read?: any;
    }): ParameterDecorator;
    new (selector: Type | string, {descendants, read}?: {
        descendants?: boolean;
        read?: any;
    }): QueryMetadata;
}

์˜ˆ์ƒ๋˜๋Š” ๋™์ž‘ :
์—„๊ฒฉํ•œ null ๊ฒ€์‚ฌ๋กœ ์ด๊ฒƒ์„ ์ปดํŒŒ์ผ ํ•  ๋•Œ ๊ตฌ์กฐํ™”๋˜์ง€ ์•Š์€ ๋ณ€์ˆ˜๊ฐ€ ์„ ํƒ ์‚ฌํ•ญ์œผ๋กœ ํ‘œ์‹œ ๋˜์—ˆ๊ธฐ ๋•Œ๋ฌธ์— ์ด๊ฒƒ์ด ์ปดํŒŒ์ผ ๋  ๊ฒƒ์œผ๋กœ ์˜ˆ์ƒํ•ฉ๋‹ˆ๋‹ค.

export interface QueryMetadataFactory {
    (selector: Type | string, whatever?: {
        descendants?: boolean;
        read?: any;
    }): ParameterDecorator;
    new (selector: Type | string, whatever?: {
        descendants?: boolean;
        read?: any;
    }): QueryMetadata;
}

์‹ค์ œ ํ–‰๋™ :
์ปดํŒŒ์ผ์‹œ :

node_modules/@angular/core/src/metadata.d.ts(356,32): error TS2459: Type '{ descendants?: boolean | undefined; read?: any; } | undefined' has no property 'descendants' and no string index signature.
node_modules/@angular/core/src/metadata.d.ts(356,45): error TS2459: Type '{ descendants?: boolean | undefined; read?: any; } | undefined' has no property 'read' and no string index signature.
node_modules/@angular/core/src/metadata.d.ts(360,36): error TS2459: Type '{ descendants?: boolean | undefined; read?: any; } | undefined' has no property 'descendants' and no string index signature.
node_modules/@angular/core/src/metadata.d.ts(360,49): error TS2459: Type '{ descendants?: boolean | undefined; read?: any; } | undefined' has no property 'read' and no string index signatu

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

์ด๊ฒƒ์€ ๊ฒฝ๊ณ„์„ ์ž…๋‹ˆ๋‹ค. ํ•จ์ˆ˜

function foo({ a, b }?: { a: number, b: number }) {
}

๊ตฌ์กฐ๋ฅผ ํ•ด์ œํ•˜๋ ค๋Š” ์ธ์ˆ˜๊ฐ€ undefined ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์‹ค์ œ๋กœ ์˜ค๋ฅ˜์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์˜ˆ์ œ์—์žˆ๋Š” ์ •ํ™•ํ•œ ์„œ๋ช…์„ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ์„ ์–ธ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ€๊ฒฝํ•˜๋ฉด

function foo({ a, b }: { a: number, b: number } = { a: 0, b: 0 }) {
}

undefined ๊ฐ€ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ๋Œ€์ฒด๋˜๋ฏ€๋กœ ๋” ์ด์ƒ ์˜ค๋ฅ˜๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํ˜ธ์ถœ์ž์˜ ๊ด€์ ์—์„œ ๋งค๊ฐœ ๋ณ€์ˆ˜๋Š” ์—ฌ์ „ํžˆ ์„ ํƒ ์‚ฌํ•ญ์ด๋ฉฐ ์ด์ œ ์„ ์–ธ ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๋ฉด ์„ ์–ธ์„ ๋ฑ‰์–ด๋ƒ…๋‹ˆ๋‹ค.

declare function foo({ a, b }?: { a: number, b: number }): void;

์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  _that_์€ ๋ฒ„๊ทธ์ž…๋‹ˆ๋‹ค.

์ œ์ณ๋‘๊ณ  ๋น„ ๊ตฌํ˜„ ์‹œ๊ทธ๋‹ˆ์ฒ˜์—์„œ ๋น„ ๊ตฌ์กฐํ™” ๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ํ”ผํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ๊ทธ๊ฒƒ๋“ค์€ ์‹ค์ œ๋กœ ๊ตฌํ˜„ ์„ธ๋ถ€ ์‚ฌํ•ญ์ž…๋‹ˆ๋‹ค. ์ฆ‰, ํ•จ์ˆ˜ ๊ตฌํ˜„์ด ์ธ์ˆ˜๋ฅผ ๊ตฌ์กฐํ™”ํ•˜๋Š”์ง€ ๋˜๋Š” ์†์„ฑ ์•ก์„ธ์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š”์ง€ ์—ฌ๋ถ€๋Š” ํ˜ธ์ถœ์ž์—๊ฒŒ ์‹ค์ œ๋กœ ๋ฌธ์ œ๊ฐ€๋˜์ง€ ์•Š์•„์•ผํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์„ ์–ธ ํŒŒ์ผ์˜ ๊ฒฝ์šฐ ๋ฐฉ์ถœ ํ•  ์‹ค์ œ ๋งค๊ฐœ ๋ณ€์ˆ˜ ์ด๋ฆ„์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— (๋งˆ์ง€ ๋ชปํ•ด) ๋น„ ๊ตฌ์กฐํ™” ํŒจํ„ด์„ ๋ฐฉ์ถœํ•˜๋ฏ€๋กœ ๋ฒ„๊ทธ๋ฅผ ์ˆ˜์ •ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

>๋ชจ๋“  ๋Œ“๊ธ€

์ด๊ฒƒ์€ ๊ฒฝ๊ณ„์„ ์ž…๋‹ˆ๋‹ค. ํ•จ์ˆ˜

function foo({ a, b }?: { a: number, b: number }) {
}

๊ตฌ์กฐ๋ฅผ ํ•ด์ œํ•˜๋ ค๋Š” ์ธ์ˆ˜๊ฐ€ undefined ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์‹ค์ œ๋กœ ์˜ค๋ฅ˜์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์˜ˆ์ œ์—์žˆ๋Š” ์ •ํ™•ํ•œ ์„œ๋ช…์„ ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์€ ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ์ด์ œ ์„ ์–ธ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ณ€๊ฒฝํ•˜๋ฉด

function foo({ a, b }: { a: number, b: number } = { a: 0, b: 0 }) {
}

undefined ๊ฐ€ ๊ธฐ๋ณธ๊ฐ’์œผ๋กœ ๋Œ€์ฒด๋˜๋ฏ€๋กœ ๋” ์ด์ƒ ์˜ค๋ฅ˜๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ํ˜ธ์ถœ์ž์˜ ๊ด€์ ์—์„œ ๋งค๊ฐœ ๋ณ€์ˆ˜๋Š” ์—ฌ์ „ํžˆ ์„ ํƒ ์‚ฌํ•ญ์ด๋ฉฐ ์ด์ œ ์„ ์–ธ ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๋ฉด ์„ ์–ธ์„ ๋ฑ‰์–ด๋ƒ…๋‹ˆ๋‹ค.

declare function foo({ a, b }?: { a: number, b: number }): void;

์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  _that_์€ ๋ฒ„๊ทธ์ž…๋‹ˆ๋‹ค.

์ œ์ณ๋‘๊ณ  ๋น„ ๊ตฌํ˜„ ์‹œ๊ทธ๋‹ˆ์ฒ˜์—์„œ ๋น„ ๊ตฌ์กฐํ™” ๋งค๊ฐœ ๋ณ€์ˆ˜๋ฅผ ํ”ผํ•˜๋Š” ๊ฒƒ์ด ์ข‹์Šต๋‹ˆ๋‹ค. ๊ทธ๊ฒƒ๋“ค์€ ์‹ค์ œ๋กœ ๊ตฌํ˜„ ์„ธ๋ถ€ ์‚ฌํ•ญ์ž…๋‹ˆ๋‹ค. ์ฆ‰, ํ•จ์ˆ˜ ๊ตฌํ˜„์ด ์ธ์ˆ˜๋ฅผ ๊ตฌ์กฐํ™”ํ•˜๋Š”์ง€ ๋˜๋Š” ์†์„ฑ ์•ก์„ธ์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋Š”์ง€ ์—ฌ๋ถ€๋Š” ํ˜ธ์ถœ์ž์—๊ฒŒ ์‹ค์ œ๋กœ ๋ฌธ์ œ๊ฐ€๋˜์ง€ ์•Š์•„์•ผํ•ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ์„ ์–ธ ํŒŒ์ผ์˜ ๊ฒฝ์šฐ ๋ฐฉ์ถœ ํ•  ์‹ค์ œ ๋งค๊ฐœ ๋ณ€์ˆ˜ ์ด๋ฆ„์ด ์—†๊ธฐ ๋•Œ๋ฌธ์— (๋งˆ์ง€ ๋ชปํ•ด) ๋น„ ๊ตฌ์กฐํ™” ํŒจํ„ด์„ ๋ฐฉ์ถœํ•˜๋ฏ€๋กœ ๋ฒ„๊ทธ๋ฅผ ์ˆ˜์ •ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰