Skip to content

TS6133 (noUnusedLocals): False positives are reported for namespaces used as types #28620

@karol-majewski

Description

@karol-majewski

TypeScript Version: 3.2.0-dev.20181117

Search Terms:

  • TS6133,
  • noUnusedLocals,
  • import types,
  • namespace,
  • keyof typeof.

Code

Set "noUnusedLocals": true in your tsconfing.json. Create two sibling files.

variables.ts

export const foo = 1;
export const bar = 2;

index.ts

import * as variables from './variables'; // 'variables' is declared but its value is never read.

export type Variable = keyof typeof variables;

Expected behavior:

Compiles without error. variables are used to create a type which is then exported, which is a valid use case.

This pattern (reading types from variables as opposed to coding the design) is often used to reduce Redux boilerplate. When your application has hundreds of actions, it's easier to write:

import { ActionType } from 'typesafe-actions';
import * as actions from './actions';

export type MyActions = ActionType<typeof actions>;

then to define each action type separately and only then create executable code.

One might argue the way to go is to write:

export type MyActions = ActionType<typeof import ('./actions')>;

but this approach has its downsides (e.g. breaks the convention of having import statements in the beginning of the file, encourages duplicate import statements when one decides to use the imported namespace in some other way later).

Actual behavior:

Error in the first line. 'variables' is declared but its value is never read.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions