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
{
"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-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-mapped-types": "on"
}
}
According to best practise.
const boundaryElement = <HTMLElement>e.target;
const boundaryElement1 = e.target as HTMLElement;
type SuperType1 = {
[Key in keyof Type]?: Type[Key];
};
type SuperType1 = Partial<Type>;
There is no difference at all.
Type[]
is the shorthand syntax for anarray
ofType
.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 |
✅ |
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.
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 x: (X | Y)[] = [];
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;
Remove useless mapped types.
type SuperType = {
[Key in keyof Type]: Type[Key];
};
type SuperType = Type;
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;
}
MIT