Skip to content

[consistent-type-imports] add option to autofix to a single type import #2769

@TwitchBronBron

Description

@TwitchBronBron

I'd like to propose a third option for the consistent-type-imports rule's prefer property. It currently has two options for prefer: type-imports and no-type-imports. However, it would be very useful to have a third option:

  • type-imports-combine: enforce all type-only imports to be import type except when there's already a concrete import from that same file, in which case prefer to combine the imports into a single statement.

Here's an example:

Logger.ts

export class Logger implements ILogger {
    log(message: string) {
        console.log(message);
    }
}
export interface ILogger {
    log(message: string);
}

main.ts

prefer: 'type-imports'

import { Logger } from './Logger';
import type { ILogger} from './Logger';
import type { SomeInterface } from './SomeInterface';

prefer: 'no-type-imports'

import { ILogger, Logger } from './Logger';
import { SomeInterface } from './SomeInterface';

New proposal:

prefer: 'type-imports-combine'

import { type ILogger, Logger } from './Logger';
import type { SomeInterface } from './SomeInterface';

Metadata

Metadata

Assignees

No one assigned

    Labels

    accepting prsGo ahead, send a pull request that resolves this issueenhancement: plugin rule optionNew rule option for an existing eslint-plugin rulepackage: eslint-pluginIssues related to @typescript-eslint/eslint-plugin

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions