43 lines
1.3 KiB
Plaintext
43 lines
1.3 KiB
Plaintext
== Why is this an issue?
|
|
|
|
Unlike in JavaScript, where every parameter can be omitted, in TypeScript you need to explicitly declare this in the function signature. Either you add ``++?++`` in the parameter declaration and ``++undefined++`` will be automatically applied to this parameter. Or you add an initializer with a default value in the parameter declaration. In the latter case, when passing ``++undefined++`` for such parameter, default value will be applied as well. So it's better to avoid passing ``++undefined++`` value to an optional or default parameter because it creates more confusion than it brings clarity. Note, that this rule is only applied to the last arguments in function call.
|
|
|
|
|
|
=== Noncompliant code example
|
|
|
|
[source,javascript]
|
|
----
|
|
function foo(x: number, y: string = "default", z?: number) {
|
|
// ...
|
|
}
|
|
|
|
foo(42, undefined); // Noncompliant
|
|
foo(42, undefined, undefined); // Noncompliant
|
|
foo(42, undefined, 5); // OK, there is no other way to force default value for second parameter
|
|
----
|
|
|
|
|
|
=== Compliant solution
|
|
|
|
[source,javascript]
|
|
----
|
|
function foo(x: number, y: string = "default", z?: number) {
|
|
// ...
|
|
}
|
|
|
|
foo(42);
|
|
----
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
include::message.adoc[]
|
|
|
|
include::highlighting.adoc[]
|
|
|
|
endif::env-github,rspecator-view[]
|