The following 39 codefixes are supported by Rehearsal, which resolve 103 different TypeScript diagnostics errors.
id: addMissingAsync
Adds the missing async
keyword where a promise should be returned.
Input:
interface Stuff {
b: () => Promise<string>;
}
export function foo(): Stuff {
return { b: () => 'hello' };
}
interface Stuff {
b: () => Promise<string>;
}
function foo(): Stuff | Date {
return { b: (_) => 'hello' };
}
const foo = <T>(x: T): string => {
await new Promise((resolve) => resolve(true));
return '';
};
Output:
interface Stuff {
b: () => Promise<string>;
}
export function foo(): Stuff {
return { b: async () => 'hello' };
}
interface Stuff {
b: () => Promise<string>;
}
function foo(): Stuff | Date {
return { b: async (_) => 'hello' };
}
const foo = async <T>(x: T): Promise<string> => {
await new Promise((resolve) => resolve(true));
return '';
};
id: addMissingAwait
Adds the missing await
keyword where a promise should be returned but not being properly handled.
Input:
export async function fn(a: Promise<() => void>) {
a();
}
async function fn(a: string, b: Promise<string>) {
const x = b;
fn(x, b);
fn(b, b);
}
Output:
export async function fn(a: Promise<() => void>) {
(await a)();
}
async function fn(a: string, b: Promise<string>) {
const x = await b;
fn(x, b);
fn(await b, b);
}
id: addMissingAwaitToInitializer
Adds missing await
keyword to declarations and expressions.
id: addMissingConst
Adds const
to all unresolved variables
Input:
export function sound() {
for (x of []) {
x;
}
}
Output:
export function sound() {
for (const x of []) {
x;
}
}
id: addMissingConstraint
Adds an extends
keyword to constrain a generic type parameter.
Input:
export function f<T>(x: T) {
const y: `${number}` = x;
y;
}
Output:
export function f<T extends `${number}`>(x: T) {
const y: `${number}` = x;
y;
}
id: addMissingDeclareProperty
Adds a declare
keyword to properties.
Input:
class B {
p = 1;
}
export class C extends B {
p: number;
}
Output:
class B {
p = 1;
}
export class C extends B {
override declare p: number;
}
id: addMissingInvocationForDecorator
Turns decorators into invocations where appropriate. Input:
declare function foo(): (...args: any[]) => void;
export class C {
@foo
bar() {}
}
Output:
declare function foo(): (...args: any[]) => void;
export class C {
@foo()
bar() {}
}
id: addMissingNewOperator
Adds new
operator where it is needed.
Input:
export class C {
constructor(_num?: number) {}
}
export var b = C(3);
Output:
export class C {
constructor(_num?: number) {}
}
export var b = new C(3);
id: addOptionalPropertyUndefined
Adds undefined
to the type of an optional field.
id: addVoidToPromise
Adds the void
as the type parameter of Promise
where appropriate.
Input:
export const p1 = new Promise((resolve) => resolve());
Output:
export const p1 = new Promise<void>((resolve) => resolve());
id: constructorForDerivedNeedSuperCall
Adds a call the super class within the constructor. Input:
class Base {}
export class C extends Base {
constructor() {}
}
Output:
class Base {}
export class C extends Base {
constructor() {
super();
}
}
id: convertToTypeOnlyExport
Adds type
to exports where the entity(s) that is exported is a type(s).
Input:
export { A, B } from '../sample-1.js';
Output:
export type { A, B } from '../sample-1.js';
id: convertTypedefToType
Converts JSDoc typedef to TypeScript type(s). Input:
/**
* @typedef {(number|string|undefined)} Foo
*/
/**
* @typedef {object} Person
* @property {object} data
* @property {string} data.name
* @property {number} data.age
* @property {object} data.contact
* @property {string} data.contact.address
* @property {string} [data.contact.phone]
*/
export class Example {
/**
* @typedef {{ count: number }} Counter
* @returns {Counter}
*/
something() {
return { count: 0 };
}
// @typedef {{ count: number }} Counter
somethingElse() {
return { count: 0 };
}
somethingDifferent() {
/**
* @typedef {{ count: number }} Counter
*/
return { count: 0 };
}
}
Output:
type Foo = (number | string | undefined);
interface Person {
data: {
name: string;
age: number;
contact: {
address: string;
phone?: string;
};
};
}
export class Example {
/**
* @typedef {{ count: number }} Counter
* @returns {Counter}
*/
something() {
return { count: 0 };
}
// @typedef {{ count: number }} Counter
somethingElse() {
return { count: 0 };
}
somethingDifferent() {
/**
* @typedef {{ count: number }} Counter
*/
return { count: 0 };
}
}
id: convertToTypeOnlyImport
Adds type
to imports where the entity(s) being imported are type(s).
Input:
import { B, C } from '../sample-1.js';
declare const b: B;
declare const c: C;
console.log(b, c);
Output:
import type { B, C } from '../sample-1.js';
declare const b: B;
declare const c: C;
console.log(b, c);
id: deleteUnmatchedParameter
Deletes parameters that are documented but not in the implementation. Input:
/**
* @param {number} a
* @param {string} b
*/
export function foo(a: number) {
console.log(a);
}
Output:
/**
* @param {number} a
*/
export function foo(a: number) {
console.log(a);
}
id: extendsInterfaceBecomesImplements
Converts extends
to implements
when interfaces are extended but empty.
Input:
interface I {}
export class C extends I {}
Output:
interface I {}
export class C implements I {}
id: fixAwaitInSyncFunction
Add async modifier to containing function. Input:
export const f = function () {
await Promise.resolve();
};
Output:
export const f = async function () {
await Promise.resolve();
};
id: fixCannotFindModule
Attempts to download type modules for modules that can not be resolved.
id: fixEnableJsxFlag
When JSX is detected, enable the tsconfig configuration for JSX.
id: fixImportNonExportedMember
Modifies the file that is being imported to expose the correct members. Input:
import { T2 } from './fail-1';
const a = 1;
const b = 1;
export { a, b };
type T2 = number;
type T1 = number;
export type { T1 };
Output:
import { T2 } from './pass-1';
const a = 1;
const b = 1;
export { a, b };
type T2 = number;
type T1 = number;
export type { T1, T2 };
id: fixMissingAttributes
Adds an attribute to a JSX element if it missing.
id: fixMissingMember
Ensures that there is a type or implementation associated with a member. Input:
export class C {
method() {
this.x = 0;
}
}
Output:
export class C {
[x: string]: number;
x: number;
method() {
this.x = 0;
}
}
id: fixMissingProperties
Adds properties that are missing from objects. Input:
interface I1 {
foo: string;
}
export const a: I1 = {};
Output:
interface I1 {
foo: string;
}
export const a: I1 = {
foo: ""
};
id: fixOverrideModifier
Adds the override
modifier where the subclass is overriding the method.
Input:
export class B {
foo(_v: string) {}
fooo(_v: string) {}
}
export class D extends B {
fooo(_v: string) {}
}
Output:
export class B {
foo(_v: string) {}
fooo(_v: string) {}
}
export class D extends B {
override fooo(_v: string) {}
}
id: fixReturnTypeInAsyncFunction
Adds Promise<...>
to the return type.
Input:
export async function fn(): null {
return null;
}
Output:
export async function fn(): Promise<null> {
return null;
}
id: import
Fixes missing imports.
id: inferFromUsage
Infers the types based on how the value is being used. Input:
export function f(x, y) {
x = 0 + x;
y = '' + y;
}
Output:
export function f(x: number, y: string) {
x = 0 + x;
y = '' + y;
}
id: invalidImportSyntax
Fixes issues related to assignability of imports.
id: jdocTypes
Annotates code with the types from the declared JSDoc comments.
id: removeUnnecessaryAwait
Removes await
where the function is not async.
Input:
export function foo() {
return 1;
}
await foo();
Output:
export function foo() {
return 1;
}
foo();
id: strictClassInitialization
Fixes classes so they are initialized correctly.
id: useDefaultImport
Converts namespaced import default imports.
id: annotateWithStrictTypeFromJSDoc
Annotates code with the only strict types from the declared JSDoc comments. Strict replacement for annotateWithTypeFromJSDoc
id: addErrorTypeGuard
Adds a cast to Error objects in catch clauses
id: addMissingTypesBasedOnInheritance
Adds types to sub-class by looking at the types from the heritage.
id: makeMemberOptional
Safely makes the access to properties optional.
id: addMissingExport
Exports members that are required and used by other exports.
id: inferReturnType
Adds the return type to methods and functions.