Skip to content
This repository has been archived by the owner on Jun 11, 2020. It is now read-only.

Compatibility with typings #173

Closed
demurgos opened this issue Oct 23, 2016 · 5 comments
Closed

Compatibility with typings #173

demurgos opened this issue Oct 23, 2016 · 5 comments

Comments

@demurgos
Copy link

demurgos commented Oct 23, 2016

Hi,
I encountered an error yesterday when trying to consume a library using @types in my main project using typings (see details). I expected that the way my dependencies handle their definitions would not break my build (that there was some "isolation" between the definitions used internally by my dependencies and my own project).

The problem is a conflict between @types/node used by the dependency and env~node (from typings) used by my main project. I published a minimal repo demonstrating the issue.

  • lib-a has an npm dependency on @types/node
  • main-project has an npm dependency on lib-a
  • main-project has a typings dependency on env~node

Trying to run tsc in main-project results in the following error:

node_modules/@types/node/index.d.ts(102,6): error TS2300: Duplicate identifier 'BufferEncoding'.
node_modules/@types/node/index.d.ts(547,9): error TS2502: 'BuffType' is referenced directly or indirectly in its own type annotation.
node_modules/@types/node/index.d.ts(548,9): error TS2502: 'SlowBuffType' is referenced directly or indirectly in its own type annotation.
node_modules/@types/node/index.d.ts(549,26): error TS2300: Duplicate identifier 'Buffer'.
node_modules/@types/node/index.d.ts(549,50): error TS2300: Duplicate identifier 'SlowBuffer'.
node_modules/@types/node/index.d.ts(570,18): error TS2300: Duplicate identifier 'EventEmitter'.
node_modules/@types/node/index.d.ts(611,22): error TS2320: Interface 'Server' cannot simultaneously extend types 'Server' and 'EventEmitter'.
  Named property 'addListener' of types 'Server' and 'EventEmitter' are not identical.
node_modules/@types/node/index.d.ts(611,22): error TS2320: Interface 'Server' cannot simultaneously extend types 'Server' and 'EventEmitter'.
  Named property 'emit' of types 'Server' and 'EventEmitter' are not identical.
node_modules/@types/node/index.d.ts(611,22): error TS2320: Interface 'Server' cannot simultaneously extend types 'Server' and 'EventEmitter'.
  Named property 'on' of types 'Server' and 'EventEmitter' are not identical.
node_modules/@types/node/index.d.ts(611,22): error TS2320: Interface 'Server' cannot simultaneously extend types 'Server' and 'EventEmitter'.
  Named property 'once' of types 'Server' and 'EventEmitter' are not identical.
node_modules/@types/node/index.d.ts(611,22): error TS2320: Interface 'Server' cannot simultaneously extend types 'Server' and 'EventEmitter'.
  Named property 'prependListener' of types 'Server' and 'EventEmitter' are not identical.
node_modules/@types/node/index.d.ts(611,22): error TS2320: Interface 'Server' cannot simultaneously extend types 'Server' and 'EventEmitter'.
  Named property 'prependOnceListener' of types 'Server' and 'EventEmitter' are not identical.
node_modules/@types/node/index.d.ts(611,22): error TS2320: Interface 'Server' cannot simultaneously extend types 'Server' and 'EventEmitter'.
  Named property 'removeListener' of types 'Server' and 'EventEmitter' are not identical.
node_modules/@types/node/index.d.ts(623,22): error TS2320: Interface 'ServerResponse' cannot simultaneously extend types 'Writable' and 'EventEmitter'.
  Named property 'addListener' of types 'Writable' and 'EventEmitter' are not identical.
node_modules/@types/node/index.d.ts(623,22): error TS2320: Interface 'ServerResponse' cannot simultaneously extend types 'Writable' and 'EventEmitter'.
  Named property 'emit' of types 'Writable' and 'EventEmitter' are not identical.
node_modules/@types/node/index.d.ts(623,22): error TS2320: Interface 'ServerResponse' cannot simultaneously extend types 'Writable' and 'EventEmitter'.
  Named property 'on' of types 'Writable' and 'EventEmitter' are not identical.
node_modules/@types/node/index.d.ts(623,22): error TS2320: Interface 'ServerResponse' cannot simultaneously extend types 'Writable' and 'EventEmitter'.
  Named property 'once' of types 'Writable' and 'EventEmitter' are not identical.
node_modules/@types/node/index.d.ts(623,22): error TS2320: Interface 'ServerResponse' cannot simultaneously extend types 'Writable' and 'EventEmitter'.
  Named property 'prependListener' of types 'Writable' and 'EventEmitter' are not identical.
node_modules/@types/node/index.d.ts(623,22): error TS2320: Interface 'ServerResponse' cannot simultaneously extend types 'Writable' and 'EventEmitter'.
  Named property 'prependOnceListener' of types 'Writable' and 'EventEmitter' are not identical.
node_modules/@types/node/index.d.ts(623,22): error TS2320: Interface 'ServerResponse' cannot simultaneously extend types 'Writable' and 'EventEmitter'.
  Named property 'removeListener' of types 'Writable' and 'EventEmitter' are not identical.
node_modules/@types/node/index.d.ts(653,22): error TS2320: Interface 'ClientRequest' cannot simultaneously extend types 'Writable' and 'EventEmitter'.
  Named property 'addListener' of types 'Writable' and 'EventEmitter' are not identical.
node_modules/@types/node/index.d.ts(653,22): error TS2320: Interface 'ClientRequest' cannot simultaneously extend types 'Writable' and 'EventEmitter'.
  Named property 'emit' of types 'Writable' and 'EventEmitter' are not identical.
node_modules/@types/node/index.d.ts(653,22): error TS2320: Interface 'ClientRequest' cannot simultaneously extend types 'Writable' and 'EventEmitter'.
  Named property 'on' of types 'Writable' and 'EventEmitter' are not identical.
node_modules/@types/node/index.d.ts(653,22): error TS2320: Interface 'ClientRequest' cannot simultaneously extend types 'Writable' and 'EventEmitter'.
  Named property 'once' of types 'Writable' and 'EventEmitter' are not identical.
node_modules/@types/node/index.d.ts(653,22): error TS2320: Interface 'ClientRequest' cannot simultaneously extend types 'Writable' and 'EventEmitter'.
  Named property 'prependListener' of types 'Writable' and 'EventEmitter' are not identical.
node_modules/@types/node/index.d.ts(653,22): error TS2320: Interface 'ClientRequest' cannot simultaneously extend types 'Writable' and 'EventEmitter'.
  Named property 'prependOnceListener' of types 'Writable' and 'EventEmitter' are not identical.
node_modules/@types/node/index.d.ts(653,22): error TS2320: Interface 'ClientRequest' cannot simultaneously extend types 'Writable' and 'EventEmitter'.
  Named property 'removeListener' of types 'Writable' and 'EventEmitter' are not identical.
node_modules/@types/node/index.d.ts(679,22): error TS2320: Interface 'IncomingMessage' cannot simultaneously extend types 'Readable' and 'EventEmitter'.
  Named property 'addListener' of types 'Readable' and 'EventEmitter' are not identical.
node_modules/@types/node/index.d.ts(679,22): error TS2320: Interface 'IncomingMessage' cannot simultaneously extend types 'Readable' and 'EventEmitter'.
  Named property 'emit' of types 'Readable' and 'EventEmitter' are not identical.
node_modules/@types/node/index.d.ts(679,22): error TS2320: Interface 'IncomingMessage' cannot simultaneously extend types 'Readable' and 'EventEmitter'.
  Named property 'on' of types 'Readable' and 'EventEmitter' are not identical.
node_modules/@types/node/index.d.ts(679,22): error TS2320: Interface 'IncomingMessage' cannot simultaneously extend types 'Readable' and 'EventEmitter'.
  Named property 'once' of types 'Readable' and 'EventEmitter' are not identical.
node_modules/@types/node/index.d.ts(679,22): error TS2320: Interface 'IncomingMessage' cannot simultaneously extend types 'Readable' and 'EventEmitter'.
  Named property 'prependListener' of types 'Readable' and 'EventEmitter' are not identical.
node_modules/@types/node/index.d.ts(679,22): error TS2320: Interface 'IncomingMessage' cannot simultaneously extend types 'Readable' and 'EventEmitter'.
  Named property 'prependOnceListener' of types 'Readable' and 'EventEmitter' are not identical.
node_modules/@types/node/index.d.ts(679,22): error TS2320: Interface 'IncomingMessage' cannot simultaneously extend types 'Readable' and 'EventEmitter'.
  Named property 'removeListener' of types 'Readable' and 'EventEmitter' are not identical.
node_modules/@types/node/index.d.ts(733,18): error TS2300: Duplicate identifier 'Agent'.
node_modules/@types/node/index.d.ts(791,18): error TS2300: Duplicate identifier 'Worker'.
node_modules/@types/node/index.d.ts(1342,22): error TS2320: Interface 'REPLServer' cannot simultaneously extend types 'Interface' and 'ReadLine'.
  Named property 'pause' of types 'Interface' and 'ReadLine' are not identical.
node_modules/@types/node/index.d.ts(1342,22): error TS2320: Interface 'REPLServer' cannot simultaneously extend types 'Interface' and 'ReadLine'.
  Named property 'resume' of types 'Interface' and 'ReadLine' are not identical.
node_modules/@types/node/index.d.ts(1377,17): error TS2300: Duplicate identifier 'CompleterResult'.
node_modules/@types/node/index.d.ts(1414,18): error TS2300: Duplicate identifier 'Script'.
node_modules/@types/node/index.d.ts(3004,22): error TS2420: Class 'Readable' incorrectly implements interface 'ReadableStream'.
  Types of property 'pause' are incompatible.
    Type '() => Readable' is not assignable to type '{ (): ReadableStream; (): void; }'.
      Type 'Readable' is not assignable to type 'ReadableStream'.
        Property 'isPaused' is missing in type 'Readable'.
node_modules/@types/node/index.d.ts(3170,22): error TS2420: Class 'Duplex' incorrectly implements interface 'ReadWriteStream'.
  Types of property 'pause' are incompatible.
    Type '() => Duplex' is not assignable to type '() => ReadWriteStream'.
      Type 'Duplex' is not assignable to type 'ReadWriteStream'.
        Property 'isPaused' is missing in type 'Duplex'.
node_modules/@types/node/index.d.ts(3191,22): error TS2420: Class 'Transform' incorrectly implements interface 'ReadWriteStream'.
  Types of property 'pause' are incompatible.
    Type '() => Transform' is not assignable to type '() => ReadWriteStream'.
      Type 'Transform' is not assignable to type 'ReadWriteStream'.
        Property 'isPaused' is missing in type 'Transform'.
node_modules/@types/node/index.d.ts(3216,5): error TS2309: An export assignment cannot be used in a module with other exported elements.
node_modules/@types/node/index.d.ts(3299,5): error TS2300: Duplicate identifier 'export='.
node_modules/@types/node/index.d.ts(3321,18): error TS2300: Duplicate identifier 'Domain'.
src/main.ts(11,13): error TS2358: The left-hand side of an 'instanceof' expression must be of type 'any', an object type or a type parameter.
typings/globals/node/index.d.ts(176,3): error TS2403: Subsequent variable declarations must have the same type.  Variable 'cache' must be of type 'any', but here has type '{ [filename: string]: NodeModule; }'.
typings/globals/node/index.d.ts(177,3): error TS2403: Subsequent variable declarations must have the same type.  Variable 'extensions' must be of type 'any', but here has type 'NodeExtensions'.
typings/globals/node/index.d.ts(202,3): error TS2403: Subsequent variable declarations must have the same type.  Variable 'parent' must be of type 'any', but here has type 'NodeModule'.
typings/globals/node/index.d.ts(205,3): error TS2403: Subsequent variable declarations must have the same type.  Variable 'children' must be of type 'any[]', but here has type 'NodeModule[]'.
typings/globals/node/index.d.ts(238,3): error TS2403: Subsequent variable declarations must have the same type.  Variable 'Console' must be of type '{ new (stdout: WritableStream, stderr?: WritableStream): Console; prototype: Console; }', but here has type 'new (stdout: WritableStream) => Console'.
typings/globals/node/index.d.ts(244,6): error TS2300: Duplicate identifier 'BufferEncoding'.
typings/globals/node/index.d.ts(252,13): error TS2403: Subsequent variable declarations must have the same type.  Variable 'Buffer' must be of type '{ new (str: string, encoding?: string): Buffer; new (size: number): Buffer; new (array: Uint8Arra...', but here has type '{ new (str: string, encoding?: string): Buffer; new (size: number): Buffer; new (array: Uint8Arra...'.
typings/globals/node/index.d.ts(386,5): error TS2403: Subsequent variable declarations must have the same type.  Variable 'errno' must be of type 'string', but here has type 'number'.
typings/globals/node/index.d.ts(469,5): error TS2403: Subsequent variable declarations must have the same type.  Variable 'env' must be of type 'any', but here has type '{ [key: string]: string; PATH: string; }'.
typings/globals/node/index.d.ts(474,5): error TS2687: All declarations of 'exitCode' must have identical modifiers.
typings/globals/node/index.d.ts(482,5): error TS2403: Subsequent variable declarations must have the same type.  Variable 'versions' must be of type 'ProcessVersions', but here has type '{ http_parser: string; node: string; v8: string; ares: string; uv: string; zlib: string; openssl:...'.
typings/globals/node/index.d.ts(673,24): error TS2300: Duplicate identifier 'Buffer'.
typings/globals/node/index.d.ts(673,48): error TS2300: Duplicate identifier 'SlowBuffer'.
typings/globals/node/index.d.ts(694,16): error TS2300: Duplicate identifier 'EventEmitter'.
typings/globals/node/index.d.ts(694,16): error TS2420: Class 'EventEmitter' incorrectly implements interface 'NodeJS.EventEmitter'.
  Property 'prependListener' is missing in type 'EventEmitter'.
typings/globals/node/index.d.ts(730,5): error TS2403: Subsequent variable declarations must have the same type.  Variable 'port' must be of type 'number', but here has type 'string | number'.
typings/globals/node/index.d.ts(735,5): error TS2403: Subsequent variable declarations must have the same type.  Variable 'headers' must be of type '{ [key: string]: any; }', but here has type 'OutgoingHeaders'.
typings/globals/node/index.d.ts(804,5): error TS2403: Subsequent variable declarations must have the same type.  Variable 'headers' must be of type 'any', but here has type 'IncomingHeaders'.
typings/globals/node/index.d.ts(806,5): error TS2403: Subsequent variable declarations must have the same type.  Variable 'trailers' must be of type 'any', but here has type 'IncomingHeaders'.
typings/globals/node/index.d.ts(807,5): error TS2403: Subsequent variable declarations must have the same type.  Variable 'rawTrailers' must be of type 'any', but here has type 'string[]'.
typings/globals/node/index.d.ts(848,16): error TS2300: Duplicate identifier 'Agent'.
typings/globals/node/index.d.ts(890,5): error TS2403: Subsequent variable declarations must have the same type.  Variable 'addressType' must be of type 'number | "udp4" | "udp6"', but here has type 'string'.
typings/globals/node/index.d.ts(893,16): error TS2300: Duplicate identifier 'Worker'.
typings/globals/node/index.d.ts(1066,5): error TS2403: Subsequent variable declarations must have the same type.  Variable 'SNICallback' must be of type '(servername: string, cb: (err: Error, ctx: SecureContext) => any) => any', but here has type '(servername: string) => any'.
typings/globals/node/index.d.ts(1070,5): error TS2403: Subsequent variable declarations must have the same type.  Variable 'pfx' must be of type 'any', but here has type 'string | Buffer'.
typings/globals/node/index.d.ts(1071,5): error TS2403: Subsequent variable declarations must have the same type.  Variable 'key' must be of type 'any', but here has type 'string | Buffer'.
typings/globals/node/index.d.ts(1073,5): error TS2403: Subsequent variable declarations must have the same type.  Variable 'cert' must be of type 'any', but here has type 'string | Buffer'.
typings/globals/node/index.d.ts(1074,5): error TS2403: Subsequent variable declarations must have the same type.  Variable 'ca' must be of type 'any', but here has type 'string | Buffer | (string | Buffer)[]'.
typings/globals/node/index.d.ts(1175,20): error TS2300: Duplicate identifier 'CompleterResult'.
typings/globals/node/index.d.ts(1222,16): error TS2300: Duplicate identifier 'Script'.
typings/globals/node/index.d.ts(2011,14): error TS2403: Subsequent variable declarations must have the same type.  Variable 'StringDecoder' must be of type 'new (encoding?: string) => NodeStringDecoder', but here has type 'new (encoding: string) => NodeStringDecoder'.
typings/globals/node/index.d.ts(2026,5): error TS2403: Subsequent variable declarations must have the same type.  Variable 'port' must be of type 'number', but here has type 'string | number'.
typings/globals/node/index.d.ts(2027,5): error TS2403: Subsequent variable declarations must have the same type.  Variable 'pfx' must be of type 'string | Buffer[]', but here has type 'string | Buffer'.
typings/globals/node/index.d.ts(2028,5): error TS2403: Subsequent variable declarations must have the same type.  Variable 'key' must be of type 'string | any[] | string[] | Buffer', but here has type 'string | Buffer'.
typings/globals/node/index.d.ts(2030,5): error TS2403: Subsequent variable declarations must have the same type.  Variable 'cert' must be of type 'string | string[] | Buffer | Buffer[]', but here has type 'string | Buffer'.
typings/globals/node/index.d.ts(2031,5): error TS2403: Subsequent variable declarations must have the same type.  Variable 'ca' must be of type 'string | string[] | Buffer | Buffer[]', but here has type 'string | Buffer | (string | Buffer)[]'.
typings/globals/node/index.d.ts(2034,5): error TS2403: Subsequent variable declarations must have the same type.  Variable 'honorCipherOrder' must be of type 'boolean', but here has type 'any'.
typings/globals/node/index.d.ts(2037,5): error TS2403: Subsequent variable declarations must have the same type.  Variable 'NPNProtocols' must be of type 'string[] | Buffer', but here has type '(string | Buffer)[]'.
typings/globals/node/index.d.ts(2038,5): error TS2403: Subsequent variable declarations must have the same type.  Variable 'SNICallback' must be of type '(servername: string, cb: (err: Error, ctx: SecureContext) => any) => any', but here has type '(servername: string) => any'.
typings/globals/node/index.d.ts(2043,5): error TS2403: Subsequent variable declarations must have the same type.  Variable 'port' must be of type 'number', but here has type 'string | number'.
typings/globals/node/index.d.ts(2046,5): error TS2403: Subsequent variable declarations must have the same type.  Variable 'key' must be of type 'string | string[] | Buffer | Buffer[]', but here has type 'string | Buffer'.
typings/globals/node/index.d.ts(2048,5): error TS2403: Subsequent variable declarations must have the same type.  Variable 'cert' must be of type 'string | string[] | Buffer | Buffer[]', but here has type 'string | Buffer'.
typings/globals/node/index.d.ts(2091,5): error TS2403: Subsequent variable declarations must have the same type.  Variable 'key' must be of type 'string | Buffer', but here has type 'string | string[] | Buffer | { pem: string; passphrase: string; }[]'.
typings/globals/node/index.d.ts(2196,5): error TS2403: Subsequent variable declarations must have the same type.  Variable 'padding' must be of type 'number', but here has type 'any'.
typings/globals/node/index.d.ts(2201,5): error TS2403: Subsequent variable declarations must have the same type.  Variable 'padding' must be of type 'number', but here has type 'any'.
typings/globals/node/index.d.ts(2220,16): error TS2420: Class 'Readable' incorrectly implements interface 'ReadableStream'.
  Types of property 'pause' are incompatible.
    Type '() => void' is not assignable to type '{ (): ReadableStream; (): void; }'.
      Type 'void' is not assignable to type 'ReadableStream'.
typings/globals/node/index.d.ts(2258,16): error TS2420: Class 'Duplex' incorrectly implements interface 'ReadWriteStream'.
  Types of property 'pause' are incompatible.
    Type '() => void' is not assignable to type '() => ReadWriteStream'.
      Type 'void' is not assignable to type 'ReadWriteStream'.
typings/globals/node/index.d.ts(2272,16): error TS2420: Class 'Transform' incorrectly implements interface 'ReadWriteStream'.
  Types of property 'pause' are incompatible.
    Type '() => void' is not assignable to type '() => ReadWriteStream'.
      Type 'void' is not assignable to type 'ReadWriteStream'.
typings/globals/node/index.d.ts(2366,3): error TS2300: Duplicate identifier 'export='.
typings/globals/node/index.d.ts(2388,16): error TS2300: Duplicate identifier 'Domain'.

My question is: how do I use @types in my library without breaking the builds of the dependant projects using typings or tsd ?

I assume that this issue is caused by the flat structure used by npm@3, but even so I don't feel that it's right that a dependency can pollute my global namespace. If in the future a second dependency lib-b that uses @types/node@7 (incompatible with the definitions for Node 6), will I have conflicts between my two dependencies ? (I think that npm will take care of the scope, but I prefer to ask).

How should Node's types be installed, as dependencies or devDependencies ? This might be related to #81, except that the definitions for Node seem to affect the whole environment: I'd prefer to be the one to install it (at the top level) and not have it dictated by dependencies. Installing it as devDependencies would solve the compatibility issue (since it is no longer installed for the consumer package) but I'm not sure if it is the intended way.

What is the solution to handle the transition ? (#4 is a blocker for me but I'd still would like to be able to consume dependencies that use @types).

@felixfbecker
Copy link

The workaround that worked for me was specifying in my tsconfig.json:

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

It will make TS2 prefer typings over node_modules/@types. But really the only reason we all cannot switch to @types is #4

@felixfbecker
Copy link

The whole pattern of adding the global node declaration as a dependency (not peerDependency) is the cause of this issue. See this build for example: https://travis-ci.org/felixfbecker/merkel/builds/166880677
typedoc switched over to @types in a minor release. typedoc itself only depends on @types/fs-extra and a couple other modules. So far so good, but @types/fs-extra has a dependency on @types/node@*. Which is a global declaration. But I already have node typings installed (through typings), so now I get duplicate identifier errors and my build is broken through a minor version update, and typedoc is not even to blame here because it didn't even depend on @types/node directly. It would have all been fine if @types/fs-extra declared a peerDependency.

This is really not only for compatibility with typings.

@blakeembrey
Copy link

@felixfbecker You might be interested in #107.

@jacobdr
Copy link

jacobdr commented Nov 11, 2016

This issue seems to have reared its head again here --> aws/aws-sdk-js#1223. Messed my day up pretty bad. The fix above did work for me though

jp7677 added a commit to jp7677/hellocoreclr that referenced this issue Jan 2, 2017
Use only the type definitions from typings since we have a conflict between global typings definitions from mocha and node @types definition from jasmine, that comes with protractor, similar to microsoft/types-publisher#173. Alternatively we could switch to jasmine completly but that's a different story.
@demurgos
Copy link
Author

I'm closing this issue since it is no longer relevant. Most of the libraries have switched to @types. As mentioned above, the typeRoots option can be used to consume other kinds of type definitions.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants