GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com โ€” putout/packages/plugin-typescript at master ยท coderaiser/putout

Expand file tree

/

plugin-typescript

/
Copy path

Directory actions

More options

More options

Directory actions

More options

More options

Latest commit

 

History

History
History
/

plugin-typescript

/
Copy path

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
lib
lib
 
 
test
test
 
 
.gitignore
.gitignore
 
 
.madrun.js
.madrun.js
 
 
.npmignore
.npmignore
 
 
.nycrc.json
.nycrc.json
 
 
.putout.json
.putout.json
 
 
LICENSE
LICENSE
 
 
README.md
README.md
 
 
eslint.config.js
eslint.config.js
 
 
package.json
package.json
 
 

README.md

Outline

@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

Rules

  • โœ… apply-as-type-assertion;
  • โœ… apply-type-guards;
  • โœ… apply-utility-types;
  • โœ… convert-commonjs-to-esm;
  • โœ… convert-empty-type-literal-to-record;
  • โœ… convert-esm-to-commonjs;
  • โœ… convert-generic-to-shorthand;
  • โœ… convert-namespace-to-global;
  • โœ… cts-file;
  • โœ… find-file;
  • โœ… mts-file;
  • โœ… remove-duplicate-exports;
  • โœ… remove-duplicate-interface-keys;
  • โœ… remove-duplicates-from-union;
  • โœ… remove-getter-arguments;
  • โœ… remove-setter-return-type;
  • โœ… remove-unused-types;
  • โœ… remove-useless-mapped-types;
  • โœ… remove-useless-mapping-modifiers;
  • โœ… remove-useless-non-null-expressions;
  • โœ… remove-useless-parens;
  • โœ… remove-useless-promise;
  • โœ… remove-useless-types;
  • โœ… remove-useless-types-from-constants;
  • โœ… rename-file-cts-to-ts;
  • โœ… rename-file-mts-to-ts;

Config

{
    "rules": {
        "typescript/apply-as-type-assertion": "on",
        
        "typescript/apply-type-guards": "on",
        "typescript/apply-utility-types": "on",
        
        "typescript/convert-commonjs-to-esm": "off",
        
        "typescript/convert-empty-type-literal-to-record": "on",
        "typescript/convert-esm-to-commonjs": "off",
        "typescript/convert-generic-to-shorthand": "on",
        
        "typescript/convert-namespace-to-global": "on",
        
        "typescript/cts-file": "off",
        
        "typescript/find-file": ["off", {
            "ignore": []
        }],
        "typescript/mts-file": "off",
        "typescript/remove-duplicates-exports": "on",
        "typescript/remove-duplicates-from-union": "on",
        "typescript/remove-duplicates-interface-keys": "on",
        
        "typescript/remove-getter-arguments": "on",
        "typescript/remove-setter-return-type": "on",
        "typescript/remove-unused-types": "on",
        
        "typescript/remove-useless-mapped-types": "on",
        "typescript/remove-useless-non-null-expressions": "on",
        "typescript/remove-useless-parens": "on",
        "typescript/remove-useless-promise": "on",
        "typescript/remove-useless-types": "on",
        "typescript/remove-useless-types-from-constants": "on",
        
        "typescript/rename-file-cts-to-ts": "off",
        "typescript/rename-file-mts-to-ts": "off"
    }
}

apply-as-type-assertion

According to best practice.

โŒ 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: unknown): 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

Linter Rule Fix
๐ŸŠ Putout typescript/convert-generic-to-shorthand โœ…
โฃ ESLint @typescript-eslint/array-type โœ…

convert-commonjs-to-esm

Checkout in ๐ŸŠPutout Editor.

โŒ Example of incorrect code

import foo = require('foo');
export = 5;

โœ… Example of correct code

import foo from 'foo';

export default 5;

convert-esm-to-commonjs

Checkout in ๐ŸŠPutout Editor.

โŒ Example of incorrect code

import foo from 'foo';

export default 5;

โœ… Example of correct code

import foo = require('foo');
export = 5;

convert-namespace-to-global

Checkout in ๐ŸŠPutout Editor.

โŒ Example of incorrect code

declare namespace global {
    var al: any;
}

โœ… Example of correct code

declare global {
    var al: any;
}

Comparison

Linter Rule Fix
๐ŸŠ Putout typescript/convert-namespace-to-global โœ…
โฃ ESLint @typescript-eslint/no-namespace โŒ

convert-empty-type-literal-to-record

{} doesn't mean an empty object, but means any types other than null and undefined

(c) deno.com

Checkout in ๐ŸŠPutout Editor.

โŒ Example of incorrect code

type TestFunction<T extends Test = Test> = ((message: string, fn: (t: T) => void) => void) & {
    extend<U extends CustomOperator = {}>(operators?: U): TestFunction<T & OperatorsToMethods<U>>;
};

โœ… Example of correct code

type TestFunction<T extends Test = Test> = ((message: string, fn: (t: T) => void) => void) & {
    extend<U extends CustomOperator = Record<PropertyKey, never>>(operators?: U): TestFunction<T & OperatorsToMethods<U>>;
};

Comparison

Linter Rule Fix
๐ŸŠ Putout typescript/convert-namespace-to-global โœ…
๐Ÿฆ• Deno bun-types โŒ

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-duplicate-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-getter-arguments

The get syntax binds an object property to a function that will be called when that property is looked up

(c) MDN

โŒ Example of incorrect code

export interface IParamsConstructor {
    get [fromArray](name: string): IParams;
}

โœ… Example of correct code

export interface IParamsConstructor {
    get [fromArray](): IParams;
}

remove-setter-return-type

The set syntax binds an object property to a function to be called when there is an attempt to set that property.

(c) MDN

โŒ Example of incorrect code

export interface IParamsConstructor {
    set fromArray(values: ParamsArray): string;
}

โœ… Example of correct code

export interface IParamsConstructor {
    set fromArray(values: ParamsArray);
}

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-non-null-expressions

Checkout in ๐ŸŠPutout Editor.

โŒ Example of incorrect code

const bar = foo!!.str;
const baz = bar!?.n;

โœ… Example of correct code

const bar = foo!.str;
const baz = bar?.n;

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

cts-file

Run convert-esm-to-commonjs for all *.cts files with help of redlint.

Check out in ๐ŸŠPutout Editor.

mts-file

Run convert-esm-to-commonjs for all *.mts files with help of redlint.

Check out in ๐ŸŠPutout Editor.

find-file

Checkout in ๐ŸŠPutout Editor.

โŒ Example of incorrect code

__putout_processor_filesystem(['/', [
    '/hello.ts',
    'const a: number = 5;',
]]);

โœ… Example of correct code

__putout_processor_filesystem([
    '/',
    ['/hello.ts', 'const a = 5;'],
]);

rename-file-cts-to-ts

Rename *.cts files when type === "commonjs":

 /
 |-- package.json
 `-- lib/
-     `-- hello.cts
+     `-- hello.ts

Check out in ๐ŸŠPutout Editor.

rename-file-mts-to-ts

Rename *.mts files when type === "module":

 /
 |-- package.json
 `-- lib/
-     `-- hello.mts
+     `-- hello.ts

Check out in ๐ŸŠPutout Editor.

License

MIT

You are visitor 01402520 โ€” GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800ร—600. No cookies required.