Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More β†’
Socket
Sign inDemoInstall
Socket

@putout/plugin-typescript

Package Overview
Dependencies
Maintainers
1
Versions
33
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@putout/plugin-typescript

🐊Putout plugin for transforming TypeScript code

  • 5.0.1
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
7.4K
decreased by-7.5%
Maintainers
1
Weekly downloads
Β 
Created
Source

@putout/plugin-typescript NPM version

TypeScript is JavaScript with syntax for types.

(c) typescriptcriptlang.org

🐊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/apply-type-guards": "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-promise": "on",
        "typescript/remove-useless-mapped-types": "on"
    }
}

Rules

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. A type guard is 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 an array of Type. Array<Type> is the generic syntax. They are completely equivalent.

(c) https://stackoverflow.com/a/36843084/4536327

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
LinterRuleFix
🐊 Putouttypescript/convert-generic-to-shorthandβœ…
⏣ ESLint@typescript-eslint/array-typeβœ…

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-duplicates-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-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-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;
}

License

MIT

Keywords

FAQs

Package last updated on 04 Aug 2023

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚑️ by Socket Inc