Research
Security News
Quasar RAT Disguised as an npm Package for Detecting Vulnerabilities in Ethereum Smart Contracts
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
@putout/plugin-typescript
Advanced tools
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/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/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": []
}]
}
}
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>;
It just so happens that TypeScript has something called a
type guard
. Atype guard
is 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): a is number => typeof a === 'number';
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 | β |
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;
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
get
syntax 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
set
syntax 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;
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
FAQs
πPutout plugin for transforming TypeScript code
The npm package @putout/plugin-typescript receives a total of 7,067 weekly downloads. As such, @putout/plugin-typescript popularity was classified as popular.
We found that @putout/plugin-typescript demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago.Β It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket researchers uncover a malicious npm package posing as a tool for detecting vulnerabilities in Etherium smart contracts.
Security News
Research
A supply chain attack on Rspack's npm packages injected cryptomining malware, potentially impacting thousands of developers.
Research
Security News
Socket researchers discovered a malware campaign on npm delivering the Skuld infostealer via typosquatted packages, exposing sensitive data.