Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[no-unused-vars] False positive: type import used in typeof #2453

Closed
3 tasks done
DisappearedStar opened this issue Sep 1, 2020 · 4 comments · Fixed by #2494
Closed
3 tasks done

[no-unused-vars] False positive: type import used in typeof #2453

DisappearedStar opened this issue Sep 1, 2020 · 4 comments · Fixed by #2494
Assignees
Labels
bug Something isn't working has pr there is a PR raised to close this package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@DisappearedStar
Copy link

  • I have tried restarting my IDE and the issue persists.
  • I have updated to the latest version of the packages.
  • I have read the FAQ and my problem is not listed.

Repro

.eslintrc.js

{
  rules: {
    'no-unused-vars': 'off',
    '@typescript-eslint/no-unused-vars': ['error', {
        varsIgnorePattern: '^_',
        argsIgnorePattern: '^_',
        ignoreRestSiblings: true,
    }],
  }
}

tsconfig.json

{
  "compilerOptions": {
    "module": "esnext",
    "moduleResolution": "node",
    "target": "es5",
    "allowJs": true,
    "importHelpers": true,
    "jsx": "react",
    "experimentalDecorators": true,
    "isolatedModules": true,
    "importsNotUsedAsValues": "error"
  }
}
// a.ts
export const foo = 42;

// b.ts
import type { foo } from './a';

export type Bar = typeof foo;

Expected Result

No lint errors

Actual Result

b.ts 1:15 'foo' is defined but never used. Allowed unused vars must match /^_/u. (@typescript-eslint/no-unused-vars)

Additional Info

Versions

package version
@typescript-eslint/eslint-plugin 4.0.1
@typescript-eslint/parser 4.0.1
TypeScript 4.0.2
ESLint 7.8.0
node 10.16.3
@DisappearedStar DisappearedStar added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels Sep 1, 2020
@bradzacher bradzacher added bug Something isn't working and removed triage Waiting for maintainers to take a look labels Sep 1, 2020
@bradzacher
Copy link
Member

This is stupidly weird and not all what I expect.
I'm really surprised that it works this way.

When I see import type { foo }, I expect that this should either:

  1. error, because foo is a value (i.e. you can't import a value as a type)
  2. be fine but foo and under the hood TS would treat it the same as typeof foo.

Instead for all intents and purposes, TS treats foo as a value, however it only allows foo be referenced in type contexts.

This breaks the assumption that I'd made that all import types are 100% types.
No biggie - easy fix, it just feels wrong.

@DisappearedStar
Copy link
Author

I confirm that the error is fixed in 4.0.2-alpha.9.

@Wolk
Copy link

Wolk commented Sep 30, 2020

Hi, i am still having this bug in 4.0.3

example of my problem

// file 1
export type FieldDefinition = {
  [key: string]: string[];
};

// file 2
interface FruitsFormDefinition {
  apple: string;
  orange: string;
}

export type FruitsFieldDefinition = { [K in keyof FruitsFormDefinition]: FieldDefinition[string] };

export const fruitsFieldDefiniton: FruitsFieldDefinition = {
  apple: [],
  orange: ['isRequired'],
};

the error i get is
Line 32:37: 'K' is defined but never used. Allowed unused vars must match /^_/u @typescript-eslint/no-unused-vars

@OriginLive
Copy link

^ made a new ticket, since it's not directly related #2615

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 31, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working has pr there is a PR raised to close this package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants