Don’t use the types Number, String, Boolean, Symbol or Object.
// Do
function reverse(s: string): string;
// Don't
function reverse(s; String): String;
Don’t use generic type T if you don’t use it! (More)
// Do
interface Named<T> {
name: string;
value: T; // <-- added
}
// Don't
interface Named<T> {
name: string;
}
Don’t use any. At least, use unknown.
Don’t return any for callbacks whose value will be ignored.
// Do
// It prevents you from accidentally using the return value of x.
function fn(x: () => void) {
x();
}
// Don't
function fn(x: () => any) {
x();
}
It’s always legal to provide a callback that accepts fewer args. So, don’t use optional params in callbacks.
// Do
interface Fetcher {
getObject(done: (data: unknown, elapsedTime: number) => void): void;
}
// Don't
interface Fetcher {
getObject(done: (data: unknown, elapsedTime?: number) => void): void;
}
Don’t overload callbacks.
// Do
declare function beforeAll(
action: (done: DoneFn) => void,
timeout?: number
): void;