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

The TS4023-TS6133 Paradox (noUnusedLocals) #24287

Closed
jeffijoe opened this issue May 21, 2018 · 8 comments · Fixed by #24330
Closed

The TS4023-TS6133 Paradox (noUnusedLocals) #24287

jeffijoe opened this issue May 21, 2018 · 8 comments · Fixed by #24330
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@jeffijoe
Copy link

TypeScript Version: 2.8.3

Search Terms: TS6133 TS4023

Code

image

image

tsconfig.json

{
  "compilerOptions": {
    "strict": true,
    "strictNullChecks": true,
    "sourceMap": true,
    "declaration": true,
    "noUnusedLocals": true
  }
}

src/a.ts

import { createNamed } from "./b";

export const Value = createNamed();

src/b.ts

export interface Named {}

export function createNamed(): Named {
  return {};
}

Expected behavior:

Preferably that the compiler will know to add the import when emitting declarations (which I am aware is the root cause of the error in the first example).

If that's not possible, then the compiler should not trigger a warning for imported types that are used for declaration emission only.

Actual behavior:

The compiler reports an error when not importing types that get implicitly exported. When adding the import to satisfy that requirement, the compiler errors because of the noUnusedLocals option. Removing the unused local (the import) causes the first error, hence the reason I aptly labeled this a paradox.

Playground Link: This error requires the use of multiple files. Therefore I have attached screenshots and sources that can be used to reproduce the exact setup.

Related Issues: #5711, #9944

@j-oliveras
Copy link
Contributor

I don't thinks that the error at Value is correct.

Disabling declaration solve the error at Value. Is this an error with declaration?

As a workaround:

import { createNamed, Named } from "./b";

export const Value: Named = createNamed();

(Named is used as a explicit type of Value).

@jeffijoe
Copy link
Author

jeffijoe commented May 21, 2018

@j-oliveras I'm aware of the fact this is caused by declaration and that I can add an explicit annotation to work around it (or disable declaration, but I want declarations). 😄

I'm raising this issue in hopes that we can work out a way to break the paradox, so that as one solves the chain of errors as the compiler suggests, you end up with compilable code.

@RyanCavanaugh RyanCavanaugh added the Bug A bug in TypeScript label May 21, 2018
@mhegazy
Copy link
Contributor

mhegazy commented May 21, 2018

The original issue is a duplicate of #9944, and should be fixed in 2.9.1.

looks like there is a diffrent bug though, the output should be:

// a.d.ts
export declare const Value: import("./b").Named;

and not

// a.d.ts
export declare const Value: Named;

@mhegazy mhegazy added this to the TypeScript 2.9.1 milestone May 21, 2018
@jeffijoe
Copy link
Author

@mhegazy ah, my bad, I didn't notice any comment indicating that it was going to get fixed.

Apologies for opening a duplicate! 🙏

@mhegazy mhegazy added the Fixed A PR has been merged for this issue label May 22, 2018
@tsofist
Copy link

tsofist commented Jun 1, 2018

@mhegazy for TS@2.9.1 this bug still reproduced with this code (with same tsconfig)

b.ts

export {
    Hash,
    StringHash, StringHash2
};

interface Hash<T> {
    [key: string]: T;
}

type StringHash = Hash<string>; // type NOT OK

interface StringHash2 extends Hash<string> { // interface OK
}

a.ts

import {StringHash, StringHash2} from "./b";

export {
    doSome
}

const MAP: StringHash = {
    a: "a"
};

const MAP2: StringHash2 = {
    a: "a"
};

function doSome(arg1: string,
                arg2 = MAP, // << type NOT OK: Error:(16, 17) TS4076: Parameter 'arg2' of exported function has or is using name 'Hash' from external module "./b" but cannot be named.
                arg3 = MAP2) { // << interface OK
    console.log(arg1, arg2, arg3);
}

Should i start a new issue?

@mhegazy
Copy link
Contributor

mhegazy commented Jun 1, 2018

Should i start a new issue?

yes please

@weswigham
Copy link
Member

weswigham commented Jun 1, 2018

@tsofist You example exposed another issue (thanks! fix is up) but your original issue is probably? a duplicate of what I fixed in #24330, given that Hash is what was trying to be written, which was an interface. Minimally, I couldn't reproduce the same issue.

@tsofist
Copy link

tsofist commented Jun 1, 2018

@weswigham Sorry, I did not notice your comment.
Therefore, the issue has already been written. The problem on @next is no longer reproduced (thanks!)

@microsoft microsoft locked and limited conversation to collaborators Aug 2, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants