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
DisappearedStar opened this issue Sep 1, 2020 · 4 comments
Closed

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

DisappearedStar opened this issue Sep 1, 2020 · 4 comments

Comments

@DisappearedStar
Copy link

@DisappearedStar DisappearedStar commented Sep 1, 2020

  • 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
@bradzacher
Copy link
Member

@bradzacher bradzacher commented Sep 5, 2020

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.

@bradzacher bradzacher self-assigned this Sep 5, 2020
@bradzacher bradzacher added the has pr label Sep 5, 2020
bradzacher added a commit that referenced this issue Sep 5, 2020
Fixes #2453
bradzacher added a commit that referenced this issue Sep 6, 2020
Fixes #2453
bradzacher added a commit that referenced this issue Sep 6, 2020
Fixes #2453
@DisappearedStar
Copy link
Author

@DisappearedStar DisappearedStar commented Sep 6, 2020

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

@Wolk
Copy link

@Wolk 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

@OriginLive OriginLive commented Sep 30, 2020

^ 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.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

4 participants