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

Consider adding a compiler option to not automatically build the inferred type names #10199

Closed
Tragetaschen opened this issue Aug 8, 2016 · 13 comments
Labels
External Relates to another program, environment, or user action which we cannot control.

Comments

@Tragetaschen
Copy link

Tragetaschen commented Aug 8, 2016

TypeScript Version: 2.0.0

I have a project with several environments where js runs and in these environments, different versions of a global require function exist:

Code

{
    "dependencies": {
        "@types/node": "6.0.31",
        "@types/phantomjs":"1.9.26",
        "typescript": "2.0.0"
    }
}
var a;

When I run node_modules\.bin\tsc test.ts, I get

node_modules/@types/node/index.d.ts(69,13): error TS2300: Duplicate identifier 'require'.
node_modules/@types/phantomjs/index.d.ts(6,18): error TS2300: Duplicate identifier 'require'.

and with --traceResolution I can see these errors originate from building the internal __inferred type names__.ts.

I can fix the error by specifying any explicit --types option (but it needs a valid argument), but AFAICS I cannot explicitly turn off building the inferred types. Might be useful.

@RyanCavanaugh RyanCavanaugh added the External Relates to another program, environment, or user action which we cannot control. label Aug 8, 2016
@RyanCavanaugh
Copy link
Member

The root cause is that phantomjs.d.ts needs to not have this line https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/phantomjs/phantomjs.d.ts#L6

and with --traceResolution I can see these errors originate from building the internal inferred type names.ts.
I cannot explicitly turn off building the inferred types. Might be useful.

This isn't really a coherent description of what's going on.

If you don't want the @types/phantomjs package, you can just not install it (?), or you can fix the .d.ts file to not have an incorrect global redefinition of require, or you can manually specify a --types list as you've discovered. Those are the options.

@Tragetaschen
Copy link
Author

Hmm,

  • I want phantomjs, because a part of my project requires it (no pun intended)
  • I want node, because another part of my project requires it
  • Both packages define a JS environment (phantomjs based on Webkit's engine, node based on V8)
  • Both have a global 'require' function in their environment, so I assume the .d.ts are correct and the require functions have nothing to do with each other.

Now, TypeScript helpfully (I mean it!) gathers all available @types packages by default and stumbles over the conflict. In that setup, my entire project is "tainted" and I can never go back to the default, so I just want to disable it.

@RyanCavanaugh
Copy link
Member

It sounds like you need to split your project into two compilations depending on which environment it's targeting. It can't be the case that two different things are setting up different globals named require at the same time.

@Tragetaschen
Copy link
Author

You are right, that's why I'm asking for a way to tell TypeScript not to do it :) Because right now, the default handling of @types creates that problem.

But you are right. It's probably the easiest to make a hard cut between these two worlds.

poelstra added a commit to poelstra/ts-stream that referenced this issue Oct 4, 2016
@felixfbecker
Copy link
Contributor

I also met this problem when using @types in conjunction with the typings definition manager:

======== Resolving type reference directive 'validator', containing file '/Users/felix/git/npm-sequelize/__inferred type names__.ts', root directory '/Users/felix/git/npm-sequelize/typings/globals
,/Users/felix/git/npm-sequelize/typings/modules'. ========
Resolving with primary search path '/Users/felix/git/npm-sequelize/typings/globals, /Users/felix/git/npm-sequelize/typings/modules'
Directory '/Users/felix/git/npm-sequelize/typings/globals' does not exist, skipping all lookups in it.
File '/Users/felix/git/npm-sequelize/typings/modules/validator/package.json' does not exist.
File '/Users/felix/git/npm-sequelize/typings/modules/validator/index.d.ts' exist - use it as a name resolution result.
Resolving real path for '/Users/felix/git/npm-sequelize/typings/modules/validator/index.d.ts', result '/Users/felix/git/npm-sequelize/typings/modules/validator/index.d.ts'
======== Type reference directive 'validator' was successfully resolved to '/Users/felix/git/npm-sequelize/typings/modules/validator/index.d.ts', primary: true. ========
node_modules/@types/validator/index.d.ts(318,3): error TS2300: Duplicate identifier 'export='.
typings/modules/validator/index.d.ts(295,1): error TS2300: Duplicate identifier 'export='.

I tried setting types to an empty array, but that doesn't work. I don't know what else I should types to. I just want TypeScript to ignore @types declarations, but not even exclude helps

@mhegazy
Copy link
Contributor

mhegazy commented Jun 7, 2017

Do you have typeRoots set?

@felixfbecker
Copy link
Contributor

Yes:

    "typeRoots": [
      "typings/globals",
      "typings/modules"
    ]

@mhegazy
Copy link
Contributor

mhegazy commented Jun 7, 2017

can you share a project or a sample?

@felixfbecker
Copy link
Contributor

felixfbecker commented Jun 7, 2017

Yeah, if you take a look here: https://github.com/types/npm-sequelize/tree/537400ae7cc7d98ccde80f6de35dcabba3963100

Those are typings for Sequelize I (as a maintainer of the Sequelize library) have been maintaining for the typings definition manager. I got countless requests to make them usable with npm / @types because those on DT are apparently not as complete, but until microsoft/types-publisher#4 is resolved, I wanted to make it possible to install them with npm from GitHub. For this I added the typings dependencies on bluebird and validator also to package.json dependencies. I added typeRoots for preferring the typings dependencies when running tests in CI, but it seems to get ignored for validator and causes the duplicate identifier error above.

@felixfbecker
Copy link
Contributor

The workaround I do is installing dependencies with npm install --only=dev, but that only works for this specific project and is confusing for contributors :/

@mhegazy
Copy link
Contributor

mhegazy commented Jun 8, 2017

add:

  "baseUrl": ".",
    "paths": {
        "*": ["typings/modules/*"]
    }

to your tsconfig.json

@felixfbecker
Copy link
Contributor

@mhegazy that works, thanks a lot!

@mhegazy
Copy link
Contributor

mhegazy commented Jun 8, 2017

Some due explanation. there are two concepts at play here. global dependencies, and modules. typeRoots manage redirecting globals like node for instance. but module imports go through a different resolution path, paths is what controls that. so typically you need to do both.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
External Relates to another program, environment, or user action which we cannot control.
Projects
None yet
Development

No branches or pull requests

4 participants