Skip to content

require-module-specifiers conflicts with TypeScript #2710

@remcohaszing

Description

@remcohaszing

TypeScript uses the presence of ESM to determine whether a file represents a module or a script. This is true for both TypeScript syntax and JavaScript syntax. The rule require-module-specifiers conflicts with this, by disallowing the export {} syntax.

These TypeScript modules define an unused function:

TypeScript playground

function fn() {}

// This makes it a module
export {}

TypeScript playground

function fn() {}

// This makes it a module.
export const variable = ''

// So this is redundant.
export {}

TypeScript playground

// Imports make it a module
import _ from 'lodash'

function fn() {}

TypeScript scripts define a global function:

TypeScript playground

function fn() {}

TypeScript playground

// Type imports don’t make it a module
import type _ from 'lodash'

function fn() {}

// Type exports don’t make it a module
export type EmptyString = ''

Setting the TypeScript option moduleDetection to force forces all files into modules. This does not affect declaration files.

I suggest to allow export {} if there is no other ESM, excluding type-only imports and exports. There’s also the rule @typescript-eslint.io/no-useless-empty-export

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions