โคNavel Piercing Madness

    @putout/plugin-typescript

    3.1.0ย โ€ขย Publicย โ€ขย Published

    @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/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"
        }
    }

    Rules

    apply-as-type-assertion

    โŒ 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>;

    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

    Linter Rule Fix
    ๐ŸŠ Putout typescript/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 x: (X | Y)[] = [];
    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-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

    Install

    npm i @putout/plugin-typescript

    DownloadsWeekly Downloads

    5,153

    Version

    3.1.0

    License

    MIT

    Unpacked Size

    22 kB

    Total Files

    16

    Last publish

    Collaborators

    • coderaiser