@putout/plugin-typescript
🐊Putout
plugin adds ability to transform TypeScript
code. Enabled by default for ts
and tsx
files.
Install
npm i putout @putout/plugin-typescript -D
Options
{
"rules": {
"typescript/apply-as-type-assertion": "on",
"typescript/apply-utility-types": "on",
"typescript/convert-generic-to-shorthand": "on",
"typescript/remove-duplicates-from-union": "on",
"typescript/remove-duplicates-interface-keys": "on",
"typescript/remove-useless-types-from-constants": "on",
"typescript/remove-unused-types": "on",
"typescript/remove-useless-types": "on",
"typescript/remove-useless-mapped-types": "on"
}
}
Rules
apply-as-type-assertion
❌ Incorrect code example
const boundaryElement = <HTMLElement>e.target;
✅ Correct code Example
const boundaryElement1 = e.target as HTMLElement;
apply-utility-types
❌ Incorrect code example
type SuperType1 = {
[Key in keyof Type]?: Type[Key];
};
✅ Correct code Example
type SuperType1 = Partial<Type>;
convert-generic-to-shorthand
Convert generic
to shorthand
(https://stackoverflow.com/a/36843084/4536327).
❌ Incorrect code example
interface A {
x: Array<X>;
y: Array<Y>;
}
✅ Correct code Example
interface A {
x: X[];
y: Y[];
}
remove-duplicates-from-union
❌ Incorrect code example
type x = boolean[]
| A
| string
| A
| string[]
| boolean[];
✅ Correct code Example
type x = boolean[]
| A
| string
| string[];
remove-useless-types-from-constants
❌ Incorrect code example
const x: any = 5;
✅ Correct code Example
const x = 5;
remove-unused-types
❌ Incorrect code example
type n = number;
type s = string;
const x: n = 5;
✅ Correct code Example
type n = number;
const x: n = 5;
remove-useless-types
❌ Incorrect code example
type oldType = {
a: number,
b: string,
};
type newType = oldType;
const x: newType = {
a: 5,
b: 'hello',
};
✅ Correct code Example
type oldType = {
a: number,
b: string,
};
const x: oldType = {
a: 5,
b: 'hello',
};
remove-useless-mapped-types
Rmove useless mapped types.
❌ Incorrect code example
type SuperType = {
[Key in keyof Type]: Type[Key];
};
✅ Correct code Example
type SuperType = Type;
remove-useless-mapping-modifiers
Remove useless mapping modifiers.
❌ Incorrect code example
type SuperType = {
[Key in keyof Type]+?: Type[Key];
};
✅ Correct code Example
type SuperType = {
[Key in keyof Type]?: Type[Key];
};
remove-duplicate-interface-keys
❌ Incorrect code example
interface Hello {
'hello': any
'hello': string
}
✅ Correct code Example
interface Hello {
'hello': string
}
License
MIT