How to enter a regular value in Function#length while specifying the optional argument in TypeScript

Asked 1 years ago, Updated 1 years ago, 83 views

In JavaScript (ECMAScript), Function.prototype.length returns the number of required parameters.

You can use arguments or rerest parameter を to specify optional arguments in ECMAScript.
arguments cannot be used in some syntax, such as class syntax, so using strest parameter を is generally a real solution.

'use strict';
function foo (...args) {
  if(args.length&&typeofargs[0]!=='string')throw new TypeError(args[0]+'is not a string');
}

function bar() {
  if(arguments.length&&type of arguments[0]!=='string')throw new TypeError(arguments[0]+'is not a string');
}

console.log (foo.length); // 0
console.log(bar.length);//0
foo();
bar();

TypeScript, on the other hand, has ? as the method for specifying optional arguments, but using this syntax, Function.prototype.length is incorrect.

// TypeScript
function foo(string?:string){}
console.log (foo.length); // 1
foo();

If you assign any type in rerest parameter で and assign each argument to a typed variable, the implementation will be the same as ECMAScript, but the variable definition is required.

// TypeScript
function foo(...args:any) {
  const string:string=args[0];
}
console.log (foo.length); // 0
foo();

Is it possible to meet the following conditions with TypeScript?

  • Function.prototype.length returns the number of required parameters
  • Type and Option Arguments with Provisional Arguments

javascript typescript

2022-09-30 20:21

2 Answers

Based on the results of the transpile, the function foo(text?:string){} in TypeScript is designed to be equivalent to function foo(text){} in JavaScript.
(It's not that the argument does not exist, but undefined is substituted if omitted at the time of the call)

The alternative is to use the spec

Include arguments with default values only before they first appear

You may want to use .
("target": "ES6" (or later) setting is required)

// TypeScript
function foo (text:string | undefined = undefined) {}
console.log (foo.length); // 0
foo();

(playground)


2022-09-30 20:21

How to use Tuple type and split substitution

If you assign any type in the prest parameter で and assign each argument to a typed variable, the implementation will be the same as ECMAScript, but a variable definition will be required.

You can use Tuple Type to ...args: [string?] instead of any.In addition, split substitutions eliminate the need for variable definitions.

 function foo (... [str]: [string?]) {
    console.log(str)
}

How to not allow the argument undefined

There are two challenges: foo(undefined) goes through and foo() and foo(undefined) cannot be distinguished. —— Comments to Kazuyuki Deba's response

This is common and is the same with Rest parameters as above.If you really want to avoid it, you can specify the number and type of arguments on a case-by-case basis with the function signature overload.

// Function Signature:
function foo() —void
function foo(str:string)—void

// Implementation:
// The call type check is not the function signature of the mounting unit below.
// It is done with the function signature above.
// Therefore, foo(undefined) is a compilation error.
function foo(...[str]: [string?]]) {
    console.log(str)
}

additional:optional argument types

foo(undefined) above is "common" because JavaScript has such a specification, of course, but the code that depends on that specification is written.I believe that TypeScript's design policy allows for strict null checks while allowing such coding without reproach.

Note: Use Optional Parameters has the context of creating a type definition file, but it is recommended that undefined be allowed rather than overloaded.


2022-09-30 20:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.