51 lines
1.0 KiB
Plaintext
51 lines
1.0 KiB
Plaintext
== Why is this an issue?
|
|
|
|
TypeScript allows declaring the type of a function in two different ways:
|
|
|
|
- Function type: `() => number`
|
|
- Object type with a call signature: `{ (): number }`
|
|
|
|
The function type syntax is generally preferred for being more concise.
|
|
|
|
== How to fix it
|
|
|
|
Use a function type instead of an interface or object type literal with a single call signature.
|
|
|
|
=== Code examples
|
|
|
|
==== Noncompliant code example
|
|
|
|
[source,typescript,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
function apply(f: { (): string }): string {
|
|
return f();
|
|
}
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,typescript,diff-id=1,diff-type=compliant]
|
|
----
|
|
function apply(f: () => string): string {
|
|
return f();
|
|
}
|
|
----
|
|
|
|
[source,typescript,diff-id=2,diff-type=noncompliant]
|
|
----
|
|
interface Foo {
|
|
(): number;
|
|
}
|
|
----
|
|
|
|
==== Compliant solution
|
|
|
|
[source,typescript,diff-id=2,diff-type=compliant]
|
|
----
|
|
type Foo = () => number;
|
|
----
|
|
|
|
== Resources
|
|
|
|
* https://www.typescriptlang.org/docs/handbook/2/functions.html#function-type-expressions[TypeScript Function Type Expressions]
|