49 lines
1.3 KiB
Plaintext
Raw Normal View History

== Why is this an issue?
2021-04-28 16:49:39 +02:00
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
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,javascript]
2021-04-28 16:49:39 +02:00
----
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
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,javascript]
2021-04-28 16:49:39 +02:00
----
function foo(x: number, y: string = "default", z?: number) {
// ...
}
foo(42);
----
ifdef::env-github,rspecator-view[]
'''
== Implementation Specification
(visible only on this page)
=== Message
Remove this redundant "undefined".
=== Highlighting
"undefined"
endif::env-github,rspecator-view[]