Rule: typedef

Requires type definitions to exist.

Notes:
TS Only

Config

Several arguments may be optionally provided:

  • "call-signature" checks return type of functions.
  • "arrow-call-signature" checks return type of arrow functions.
  • "parameter" checks type specifier of function parameters for non-arrow functions.
  • "arrow-parameter" checks type specifier of function parameters for arrow functions.
  • "property-declaration" checks return types of interface properties.
  • "variable-declaration" checks non-binding variable declarations.
  • "variable-declaration-ignore-function" ignore variable declarations for non-arrow and arrow functions.
  • "member-variable-declaration" checks member variable declarations.
  • "object-destructuring" checks object destructuring declarations.
  • "array-destructuring" checks array destructuring declarations.
Config examples
"typedef": [true, "call-signature", "parameter", "member-variable-declaration"]
Schema
{
  "type": "array",
  "items": {
    "type": "string",
    "enum": [
      "call-signature",
      "arrow-call-signature",
      "parameter",
      "arrow-parameter",
      "property-declaration",
      "variable-declaration",
      "variable-declaration-ignore-function",
      "member-variable-declaration",
      "object-destructuring",
      "array-destructuring"
    ]
  },
  "minLength": 0,
  "maxLength": 10
}

Code examples:

Requires type definitions for call signatures
"rules": { "typedef": [true, "call-signature"] }
Passes
function add(x, y): number {
    return x + y;
}
Fails
function add(x, y) {
    return x + y;
}
Requires type definitions for arrow call signatures
"rules": { "typedef": [true, "arrow-call-signature"] }
Passes
const add = (x, y): number => x + y;
Fails
const add = (x, y) => x + y;
Requires type definitions for parameters
"rules": { "typedef": [true, "parameter"] }
Passes
function add(x: number, y: number) {
    return x + y;
}
Fails
function add(x, y) {
    return x + y;
}
Requires type definitions for arrow function parameters
"rules": { "typedef": [true, "arrow-parameter"] }
Passes
const add = (x: number, y: number) => x + y;
Fails
const add = (x, y) => x + y;
Requires type definitions for property declarations
"rules": { "typedef": [true, "property-declaration"] }
Passes
interface I {
    foo: number;
    bar: string;
}
Fails
interface I {
    foo;
    bar;
}
Requires type definitions for variable declarations
"rules": { "typedef": [true, "variable-declaration"] }
Passes
let x: number;
Fails
let x;
Requires type definitions for member variable declarations
"rules": { "typedef": [true, "member-variable-declaration"] }
Passes
class MyClass {
    x: number;
}
Fails
class MyClass {
    x;
}
Requires type definitions when destructuring objects.
"rules": { "typedef": [true, "object-destructuring"] }
Passes
interface FooBar {
    foo: number;
    bar: string;
}
const foobar = { foo: 1, bar: '2' };
const { foo, bar }: FooBar = foobar;
Fails
interface FooBar {
    foo: number;
    bar: string;
}
const foobar = { foo: 1, bar: '2' };
const { foo, bar } = foobar;
Requires type definitions when destructuring arrays.
"rules": { "typedef": [true, "array-destructuring"] }
Passes
const foobar = [1, '2'];
const [foo, bar]: Array<number | string> = foobar;
Fails
const foobar = [1, '2'];
const [foo, bar] = foobar;