src/content/docs/linter/rules/use-shorthand-function-type.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="TypeScript and TSX" icon="seti:typescript"> ## Summary - Rule available since: `v1.5.0` - Diagnostic Category: [`lint/style/useShorthandFunctionType`](/reference/diagnostics#diagnostic-category) - This rule is **recommended**, meaning it is enabled by default. - This rule has a [**safe**](/linter/#safe-fixes) fix. - The default severity of this rule is [**information**](/reference/diagnostics#information). - Sources: - Same as [`@typescript-eslint/prefer-function-type`](https://typescript-eslint.io/rules/prefer-function-type){
"linter": {
"rules": {
"style": {
"useShorthandFunctionType": "error"
}
}
}
}
Enforce using function types instead of object type with call signatures.
TypeScript allows for two common ways to declare a type for a function:
() => string{ (): string }The function type form is generally preferred when possible for being more succinct.
This rule suggests using a function type instead of an interface or object type literal with a single call signature.
interface Example {
(): string;
}
function foo(example: { (): number }): number {
return example();
}
type Example = () => string;
function foo(example: () => number): number {
return bar();
}
// returns the function itself, not the `this` argument.
type ReturnsSelf2 = (arg: string) => ReturnsSelf;
interface Foo {
bar: string;
}
interface Bar extends Foo {
(): void;
}
// multiple call signatures (overloads) is allowed:
interface Overloaded {
(data: string): number;
(id: number): string;
}
// this is equivalent to Overloaded interface.
type Intersection = ((data: string) => number) & ((id: number) => string);