Closed yusunghyun closed 1 year ago
Thanks for the PR!
This section of the codebase is owned by @bumkeyy, @yeonjuan, @guyeol, and @dvlprsh - if they write a comment saying "LGTM" then it will be merged.
### Rest Parameter We can use optional parameters and overloads to accept a variety of fixed arguments, but we can use _Undefined_ A function that accepts a number argument _The rest of the parameters_It can be defined using . The rest of the parameters appear after all other parameters, `...` Use syntax. ```ts twoslash function multiply(n: number, ...m: number[]) { return m.map((x) => n * x); } // 'a' gets value [10, 20, 30, 40] const a = multiply(10, 1, 2, 3, 4); ``` In TypeScript, the type notation for these parameters is implicitly `any`Rather than `any[]`, and the type expression is `Array
Parameter decomposition can be used to conveniently unpack an object provided as an argument into one or more local variables in the body of the function. In JavaScript, it looks like this: ```js function sum({ a, b, c }) { console.log(a + b + c); } sum({ a: 10, b: 3, c: 9 }); ``` The type notation for the object is located after the decomposition syntax. ```ts twoslash function sum({ a, b, c }: { a: number; b: number; c: number }) { console.log(a + b + c); } ``` It may seem a bit verbose, but you can use the named type here as well. ```ts twoslash // 이전 예제와 동일 type ABC = { a: number; b: number; c: number }; function sum({ a, b, c }: ABC) { console.log(a + b + c); } ``` ## Assignability of functions ### `void` Return Type hydrous `void` Return types can cause some uncommon but predictable behavior. `void` Contextual typing with return types returns nothing to the function **Avoid** Not forced **Is**Another way to explain this is, `void` A contextual function type with a return type (`type vf = () => void`) is implemented, _No value_or may be returned, but will be ignored. Therefore, the type to be described later `() => void`All implementations are valid. ```ts twoslash type voidFunc = () => void; const f1: voidFunc = () => { return true; }; const f2: voidFunc = () => true; const f3: voidFunc = function () { return true; }; ``` And when the return values of these functions are assigned to other variables, they are still `void`We will keep the type. ```ts twoslash type voidFunc = () => void; const f1: voidFunc = () => { return true; }; const f2: voidFunc = () => true; const f3: voidFunc = function () { return true; }; // ---cut--- const v1 = f1(); const v2 = f2(); const v3 = f3(); ``` Since such behavior exists, `Array.prototype.push`returns a number, `Array.prototype.forEach` Method `void` Even if you expect a function with a return type, the following code may be valid: ```ts twoslash const src = [1, 2, 3]; const dst = [0]; src.forEach((el) => dst.push(el)); ``` There is one other case to keep in mind. If the literal function definition is `void` If it has a return value, the function must return nothing. **Not**. ```ts twoslash function f2(): void { // @ts-expect-error return true; } const f3 = function (): void { // @ts-expect-error return true; }; ``` `void`For more information, refer to the following article topics. - [v1 handbook](https://www.typescriptlang.org/docs/handbook/basic-types.html#void) - [v2 handbook](https://www.typescriptlang.org/docs/handbook/2/functions.html#void) - [FAQ - "Why are functions returning non-void assignable to function returning void?"](https://github.com/Microsoft/TypeScript/wiki/FAQ#why-are-functions-returning-non-void-assignable-to-function-returning-void)배경지식 읽기:
구조분해 할당
Generated by :no_entry_sign: dangerJS against 9d86fb2ba8c4d648c36959bb5076bb71ae42e4f9
@microsoft-github-policy-service agree
LGTM
Merging because @dvlprsh is a code-owner of all the changes - thanks!
Fix Typo