TypeScript is JavaScript with syntax for types.
๐Putout plugin adds ability to transform TypeScript code. Enabled by default for ts and tsx files.
npm i putout @putout/plugin-typescript -D
- โ apply-as-type-assertion;
- โ apply-type-guards;
- โ apply-utility-types;
- โ convert-commonjs-to-esm;
- โ convert-empty-type-literal-to-record;
- โ convert-esm-to-commonjs;
- โ convert-generic-to-shorthand;
- โ convert-namespace-to-global;
- โ cts-file;
- โ find-file;
- โ mts-file;
- โ remove-duplicate-exports;
- โ remove-duplicate-interface-keys;
- โ remove-duplicates-from-union;
- โ remove-getter-arguments;
- โ remove-setter-return-type;
- โ remove-unused-types;
- โ remove-useless-mapped-types;
- โ remove-useless-mapping-modifiers;
- โ remove-useless-non-null-expressions;
- โ remove-useless-parens;
- โ remove-useless-promise;
- โ remove-useless-types;
- โ remove-useless-types-from-constants;
- โ rename-file-cts-to-ts;
- โ rename-file-mts-to-ts;
{
"rules": {
"typescript/apply-as-type-assertion": "on",
"typescript/apply-type-guards": "on",
"typescript/apply-utility-types": "on",
"typescript/convert-commonjs-to-esm": "off",
"typescript/convert-empty-type-literal-to-record": "on",
"typescript/convert-esm-to-commonjs": "off",
"typescript/convert-generic-to-shorthand": "on",
"typescript/convert-namespace-to-global": "on",
"typescript/cts-file": "off",
"typescript/find-file": ["off", {
"ignore": []
}],
"typescript/mts-file": "off",
"typescript/remove-duplicates-exports": "on",
"typescript/remove-duplicates-from-union": "on",
"typescript/remove-duplicates-interface-keys": "on",
"typescript/remove-getter-arguments": "on",
"typescript/remove-setter-return-type": "on",
"typescript/remove-unused-types": "on",
"typescript/remove-useless-mapped-types": "on",
"typescript/remove-useless-non-null-expressions": "on",
"typescript/remove-useless-parens": "on",
"typescript/remove-useless-promise": "on",
"typescript/remove-useless-types": "on",
"typescript/remove-useless-types-from-constants": "on",
"typescript/rename-file-cts-to-ts": "off",
"typescript/rename-file-mts-to-ts": "off"
}
}According to best practice.
const boundaryElement = <HTMLElement>e.target;
const boundaryElement1 = e.target as HTMLElement;
type SuperType1 = { [Key in keyof Type]?: Type[Key]; };
type SuperType1 = Partial<Type>;
It just so happens that TypeScript has something called a
type guard. Atype guardis some expression that performs a runtime check that guarantees the type in some scope.(c) typescript.org
Check out in ๐Putout Editor.
const isNumber = (a) => typeof a === 'number';
const isNumber = (a: unknown): a is number => typeof a === 'number';
There is no difference at all.
Type[]is the shorthand syntax for anarrayofType.Array<Type>is the generic syntax. They are completely equivalent.
Convert generic to shorthand.
interface A { x: Array<X>; y: Array<X | Y>; }
interface A { x: X[]; y: X[] | Y[]; }
| Linter | Rule | Fix |
|---|---|---|
| ๐ Putout | typescript/convert-generic-to-shorthand |
โ |
| โฃ ESLint | @typescript-eslint/array-type |
โ |
Checkout in ๐Putout Editor.
import foo = require('foo'); export = 5;
import foo from 'foo'; export default 5;
Checkout in ๐Putout Editor.
import foo from 'foo'; export default 5;
import foo = require('foo'); export = 5;
Checkout in ๐Putout Editor.
declare namespace global { var al: any; }
declare global { var al: any; }
| Linter | Rule | Fix |
|---|---|---|
| ๐ Putout | typescript/convert-namespace-to-global |
โ |
| โฃ ESLint | @typescript-eslint/no-namespace |
โ |
{}doesn't mean an empty object, but means any types other thannullandundefined(c) deno.com
Checkout in ๐Putout Editor.
type TestFunction<T extends Test = Test> = ((message: string, fn: (t: T) => void) => void) & { extend<U extends CustomOperator = {}>(operators?: U): TestFunction<T & OperatorsToMethods<U>>; };
type TestFunction<T extends Test = Test> = ((message: string, fn: (t: T) => void) => void) & { extend<U extends CustomOperator = Record<PropertyKey, never>>(operators?: U): TestFunction<T & OperatorsToMethods<U>>; };
| Linter | Rule | Fix |
|---|---|---|
| ๐ Putout | typescript/convert-namespace-to-global |
โ |
| ๐ฆ Deno | bun-types |
โ |
type x = | boolean[] | A | string | A | string[] | boolean[];
type x = | boolean[] | A | string | string[];
In JavaScript duplicate exports leads to SyntaxError, anyways TypeScript parses such code and reports Duplicates Identifier diagnostic.
It gives us ability to automate fixing of such code ๐. Check it out in ๐Putout Editor.
export { a, hello, a, world, };
export { hello, a, world, };
โ๏ธ The rule fits good with putout/add-newlines-between-specifiers of eslint-plugin-putout.
The
getsyntax binds an object property to a function that will be called when that property is looked up(c) MDN
export interface IParamsConstructor { get [fromArray](name: string): IParams; }
export interface IParamsConstructor { get [fromArray](): IParams; }
The
setsyntax binds an object property to a function to be called when there is an attempt to set that property.(c) MDN
export interface IParamsConstructor { set fromArray(values: ParamsArray): string; }
export interface IParamsConstructor { set fromArray(values: ParamsArray); }
const x: any = 5;
const x = 5;
type n = number; type s = string; const x: n = 5;
type n = number; const x: n = 5;
type oldType = { a: number; b: string; }; type newType = oldType; const x: newType = { a: 5, b: 'hello', };
type oldType = { a: number; b: string; }; const x: oldType = { a: 5, b: 'hello', };
Check it out in ๐Putout Editor.
const m: X[] = []; const z: X | Y = 5; const f: X = 5;
const x: X[] | Y[] = []; const m: X[] = []; const z: X | Y = 5; const f: X = 5;
Check it out in ๐Putout Editor.
function doStuff(): Promise<string> { return 'hello'; }
function doStuff(): string { return 'hello'; }
Remove useless mapped types.
type SuperType = { [Key in keyof Type]: Type[Key]; };
type SuperType = Type;
Checkout in ๐Putout Editor.
const bar = foo!!.str; const baz = bar!?.n;
const bar = foo!.str; const baz = bar?.n;
Remove useless mapping modifiers.
type SuperType = { [Key in keyof Type]+?: Type[Key]; };
type SuperType = { [Key in keyof Type]?: Type[Key]; };
interface Hello { hello: any; hello: string; }
interface Hello { hello: string; }
Run convert-esm-to-commonjs for all *.cts files with help of redlint.
Check out in ๐Putout Editor.
Run convert-esm-to-commonjs for all *.mts files with help of redlint.
Check out in ๐Putout Editor.
Checkout in ๐Putout Editor.
__putout_processor_filesystem(['/', [ '/hello.ts', 'const a: number = 5;', ]]);
__putout_processor_filesystem([ '/', ['/hello.ts', 'const a = 5;'], ]);
Rename *.cts files when type === "commonjs":
/ |-- package.json `-- lib/ - `-- hello.cts + `-- hello.ts
Check out in ๐Putout Editor.
Rename *.mts files when type === "module":
/ |-- package.json `-- lib/ - `-- hello.mts + `-- hello.ts
Check out in ๐Putout Editor.
MIT