@putout/plugin-typescript v12.1.2
@putout/plugin-typescript 
TypeScript is JavaScript with syntax for types.
πPutout plugin adds ability to transform TypeScript code. Enabled by default for ts and tsx files.
Install
npm i putout @putout/plugin-typescript -DRules
- β apply-as-type-assertion;
- β apply-type-guards;
- β apply-utility-types;
- β convert-commonjs-to-esm;
- β convert-esm-to-commonjs;
- β convert-generic-to-shorthand;
- β cts-file;
- β find-file;
- β mts-file;
- β remove-duplicate-exports;
- β remove-duplicate-interface-keys;
- β remove-duplicates-from-union;
- β remove-unused-types;
- β remove-getter-arguments;
- β remove-setter-return-type;
- β 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;
Config
{
"rules": {
"typescript/apply-as-type-assertion": "on",
"typescript/apply-utility-types": "on",
"typescript/apply-type-guards": "on",
"typescript/convert-generic-to-shorthand": "on",
"typescript/convert-commonjs-to-esm": "off",
"typescript/convert-esm-to-commonjs": "off",
"typescript/remove-duplicates-from-union": "on",
"typescript/remove-duplicates-interface-keys": "on",
"typescript/remove-duplicates-exports": "on",
"typescript/remove-useless-types-from-constants": "on",
"typescript/remove-unused-types": "on",
"typescript/remove-useless-types": "on",
"typescript/remove-useless-parens": "on",
"typescript/remove-useless-promise": "on",
"typescript/remove-getter-arguments": "on",
"typescript/remove-setter-return-type": "on",
"typescript/remove-useless-mapped-types": "on",
"typescript/remove-useless-non-null-expressions": "on",
"typescript/cts-file": "off",
"typescript/mts-file": "off",
"typescript/rename-file-cts-to-ts": "off",
"typescript/rename-file-mts-to-ts": "off",
"typescript/find-file": ["off", {
"ignore": []
}]
}
}apply-as-type-assertion
According to best practise.
β Example of incorrect code
const boundaryElement = <HTMLElement>e.target;β Example of correct code
const boundaryElement1 = e.target as HTMLElement;apply-utility-types
β Example of incorrect code
type SuperType1 = {
[Key in keyof Type]?: Type[Key];
};β Example of correct code
type SuperType1 = Partial<Type>;apply-type-guards
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.
β Example of incorrect code
const isNumber = (a) => typeof a === 'number';β Example of correct code
const isNumber = (a): a is number => typeof a === 'number';convert-generic-to-shorthand
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.
β Example of incorrect code
interface A {
x: Array<X>;
y: Array<X | Y>;
}β Example of correct code
interface A {
x: X[];
y: X[] | Y[];
}Comparison
| Linter | Rule | Fix |
|---|---|---|
| π Putout | typescript/convert-generic-to-shorthand | β |
| β£ ESLint | @typescript-eslint/array-type | β |
convert-commonjs-to-esm
Checkout in πPutout Editor.
β Example of incorrect code
import foo = require('foo');
export = 5;β Example of correct code
import foo from 'foo';
export default 5;convert-esm-to-commonjs
Checkout in πPutout Editor.
β Example of incorrect code
import foo from 'foo';
export default 5;β Example of correct code
import foo = require('foo');
export = 5;remove-duplicates-from-union
β Example of incorrect code
type x =
| boolean[]
| A
| string
| A
| string[]
| boolean[];β Example of correct code
type x =
| boolean[]
| A
| string
| string[];remove-duplicate-exports
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.
β Example of incorrect code
export {
a,
hello,
a,
world,
};β Example of correct code
export {
hello,
a,
world,
};βοΈ The rule fits good with putout/add-newlines-between-specifiers of eslint-plugin-putout.
remove-getter-arguments
The
getsyntax binds an object property to a function that will be called when that property is looked up(c) MDN
β Example of incorrect code
export interface IParamsConstructor {
get [fromArray](name: string): IParams;
}β Example of correct code
export interface IParamsConstructor {
get [fromArray](): IParams;
}remove-setter-return-type
The
setsyntax binds an object property to a function to be called when there is an attempt to set that property.(c) MDN
β Example of incorrect code
export interface IParamsConstructor {
set fromArray(values: ParamsArray): string;
}β Example of correct code
export interface IParamsConstructor {
set fromArray(values: ParamsArray);
}remove-useless-types-from-constants
β Example of incorrect code
const x: any = 5;β Example of correct code
const x = 5;remove-unused-types
β Example of incorrect code
type n = number;
type s = string;
const x: n = 5;β Example of correct code
type n = number;
const x: n = 5;remove-useless-types
β Example of incorrect code
type oldType = {
a: number;
b: string;
};
type newType = oldType;
const x: newType = {
a: 5,
b: 'hello',
};β Example of correct code
type oldType = {
a: number;
b: string;
};
const x: oldType = {
a: 5,
b: 'hello',
};remove-useless-parens
Check it out in πPutout Editor.
β Example of incorrect code
const m: X[] = [];
const z: (X | Y) = 5;
const f: X = 5;β Example of correct code
const x: X[] | Y[] = [];
const m: X[] = [];
const z: X | Y = 5;
const f: X = 5;remove-useless-promise
Check it out in πPutout Editor.
β Example of incorrect code
function doStuff(): Promise<string> {
return 'hello';
}β Example of correct code
function doStuff(): string {
return 'hello';
}remove-useless-mapped-types
Remove useless mapped types.
β Example of incorrect code
type SuperType = {
[Key in keyof Type]: Type[Key];
};β Example of correct code
type SuperType = Type;remove-useless-non-null-expressions
Checkout in πPutout Editor.
β Example of incorrect code
const bar = foo!!.str;
const baz = bar!?.n;β Example of correct code
const bar = foo!.str;
const baz = bar?.n;remove-useless-mapping-modifiers
Remove useless mapping modifiers.
β Example of incorrect code
type SuperType = {
[Key in keyof Type]+?: Type[Key];
};β Example of correct code
type SuperType = {
[Key in keyof Type]?: Type[Key];
};remove-duplicate-interface-keys
β Example of incorrect code
interface Hello {
hello: any;
hello: string;
}β Example of correct code
interface Hello {
hello: string;
}cts-file
Run convert-esm-to-commonjs for all *.cts files with help of redlint.
Check out in πPutout Editor.
mts-file
Run convert-esm-to-commonjs for all *.mts files with help of redlint.
Check out in πPutout Editor.
find-file
Checkout in πPutout Editor.
β Example of incorrect code
__putout_processor_filesystem(['/', [
'/hello.ts',
'const a: number = 5;',
]]);β Example of correct code
__putout_processor_filesystem(['/', [
'/hello.ts',
'const a = 5;',
]]);rename-file-cts-to-ts
Rename *.cts files when type === "commonjs":
/
|-- package.json
`-- lib/
- `-- hello.cts
+ `-- hello.tsCheck out in πPutout Editor.
rename-file-mts-to-ts
Rename *.mts files when type === "module":
/
|-- package.json
`-- lib/
- `-- hello.mts
+ `-- hello.tsCheck out in πPutout Editor.
License
MIT
10 months ago
5 months ago
7 months ago
6 months ago
5 months ago
11 months ago
9 months ago
8 months ago
9 months ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
3 years ago
3 years ago
3 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago