Duplicate type declarations with npm link
Using TypeScript 1.7.3.
Suppose I have the below npm packages. The declaration files are generated by TypeScript compiler, and referred to from the other packages by means of the way described here.
package-a
ts src:
export default class ClassA {
private foo: string;
bar: number;
}
ts declaration:
declare class ClassA {
private foo;
bar: number;
}
export default ClassA;
package-b (depends on package-a):
ts src:
import ClassA from 'package-a';
namespace ClassAFactory {
export function create(): ClassA {
return new ClassA();
}
}
export default ClassAFactory;
ts declaration:
import ClassA from 'package-a';
declare namespace ClassAFactory {
function create(): ClassA;
}
export default ClassAFactory;
package-c (depends on package-a and package-b):
ts src:
import ClassA from 'package-a';
import ClassAFactory from 'package-b';
let classA: ClassA;
classA = ClassAFactory.create(); // error!!
The last line causes an error during compilation:
error TS2322: Type 'ClassA' is not assignable to type 'ClassA'.
Types have separate declarations of a private property 'foo'.
When I remove the line private foo; from the declaration of package-a, TypeScript does not emit any error.
However this workaround is a bit painful.
I understand that exposing private properties to declaration is by design (https://github.com/Microsoft/TypeScript/issues/1532). I think TypeScript should ignore private properties when compiling variable assignment. Or is there any better workaround for this?
There's only one root declaration of ClassA here, so this error shouldn't occur.
Well, sorry I found that this is related to npm link.
When I use npm link, packages are installed as below, as it simply creates symbolic links.
package-c
|
-- node_modules
|
-- package-a
| |
| -- index.d.ts
| |
| ...
|
-- package-b
|
-- index.d.ts
|
-- node_modules
| |
| -- package-a
| |
| -- index.d.ts
| |
| ...
|
...
As shown, it looks like there are two different declaration files for package-a.
If I install packages normally by using npm install, this does not happen because the declaration of package-a is not included in package-b in this case.
I hope there would be some solution for this anyway, but it might be difficult and low priority.
I ended up not using npm link, and this does not matter any more for me.
Fair enough, but someone else might :wink:
there are actually two files on disk with two declarations of ClassA. so the error is correct. but we need to consider node modules when we compare these types. this issue has been reported before in https://github.com/Microsoft/TypeScript/issues/4800, for Enums we changed the rule to a semi-nominal check. possibly do the same for classes.
+1 on this with TS 1.7.5 with all relevant packages NPM-linked. I tried to construct a testcase that exhibits the issue but could not. No matter what I tried, TS was fine with the scenario I see failing with TS2345 in my application, and as far as I could tell, all copies of the problematic .d.ts file were symlinks to the same file, so there should not have been differing declarations within the type. It would be nice however if the error emitted by Typescript referenced the files which declared the two incompatible types, as that might shed light on something I'm not considering. Right now it says there are two definitions but does nothing to help the developer pinpoint the issue.
As a workaround you can use <any> on the conflicting expression to skip the type check. Obviously this might require you to do another type annotation where you might not have had to before. I hope someone can isolate this issue at some point.
EDIT: made it clear that NPM link is at play in my case
Noticed TS 1.8 is available, upgraded and the issue still exists in that version as well.
Thanks for all the work in analyzing and documenting this issue. We're having the same problem in some of our code bases. We ported some projects to properly use package.json dependencies but are now seeing this when using npm link during development.
Is there anything I can help to solve this issue?
I'm using Lerna which symlinks packages around and seeing the issue there as well. Typescript version 2.0.3.
Unfortunately Lerna and its symlinks are a hard requirement, so I used this nasty workaround to get this to compile fine while still being properly type-checkable by consumers:
export class MyClass {
constructor(foo: Foo) {
(this as any)._foo = foo;
}
get foo() {
return (this as any)._foo as Foo;
}
}
The class is very small so it wasn't that arduous, and I don't expect it to change really ever, which is why I consider this an acceptable workaround.
FYI, I've also ended up here as a result of using npm link and getting this error. Has anybody found a workaround for this?
@xogeny can you elaborate on how npm link is causing this issue for you?
@mhegazy Well I started getting these errors like the one above (except I was using Observable from rxjs, i.e., "Type 'Observable' is not assignable to type 'Observable'). This, of course, seemed odd because the two I was referencing Observable from exactly the same version of rxjs in both modules. But where the types "met", I got an error. I dug around and eventually found this issue where @kimamula pointed out that if you use npm link, you'll get this error. I, like others, worked around this (in my case, I created a duplicate interface of just the functionality I needed in one module, rather than references rxjs).
Does that answer your question? I ask because I don't think my case appears any different than the others here so I'm not sure if this helps you.
We have done work in TS2.0 specifically to enable npm link scenarios (see https://github.com/Microsoft/TypeScript/pull/8486 and #8346). Do you have a sample where i can look at where npm link is still not working for you?
Huh. I'm running 2.0.3 (I checked). I'll try to create a reproducible case.
By the way, you should follow up on these threads since they imply that this is still an issue as of TS 2.0:
https://github.com/ReactiveX/rxjs/issues/1858 https://github.com/ReactiveX/rxjs/issues/1744
The issue I'm seeing in my Lerna repo is somewhat involved, so I made a stripped-down version of it at https://github.com/seansfkelley/typescript-lerna-webpack-sadness. It might even be webpack/ts-loader's fault, so I've filed https://github.com/TypeStrong/ts-loader/issues/324 over there as well.
I'm using typescript 2.0.3 and I'm seeing this error with Observable as described above, e.g.
Type 'Observable<Location[]>' is not assignable to type 'Observable<Location[]>'. Property
'source' is protected but type 'Observable<T>' is not a class derived from 'Observable<T>'.
I am hitting this in a Lerna monorepo package as well. It feels like most but not all parts of the type system are using the realpath to uniquely identify files. If you travel down the branch that is using the symlink path rather than the realpath, you'll end up with identical-but-different types.
This is a pretty brutal problem that will only affect more complex codebases, and it seems impossible to work around without taking drastic measures, so I hope I can convince you all to give it the attention it deserves. 😄
It's most noticeable in cases where you have an app that depends on Dependency A, Dependency A depends on Dependency B and vends objects that contain types from Dependency B. The app and Dependency A both npm link Dependency B and expect to be able to import types from it and have them describe the same thing.
This results in deep error messages, and I'm on the verge of going through and eliminating all of the private and protected properties in my libraries because I've already lost so much time to this:
TSError: ⨯ Unable to compile TypeScript
tests/helpers/test-application.ts (71,11): Argument of type '{ initializers: Initializer[]; rootPath: string; }' is not assignable to parameter of type 'ConstructorOptions'.
Types of property 'initializers' are incompatible.
Type 'Initializer[]' is not assignable to type 'Initializer[]'.
Type 'Application.Initializer' is not assignable to type 'Application.Initializer'.
Types of property 'initialize' are incompatible.
Type '(app: Application) => void' is not assignable to type '(app: Application) => void'.
Types of parameters 'app' and 'app' are incompatible.
Type 'Application' is not assignable to type 'Application'.
Types of property 'container' are incompatible.
Type 'Container' is not assignable to type 'Container'.
Types of property 'resolver' are incompatible.
Type 'Resolver' is not assignable to type 'Resolver'.
Types of property 'ui' are incompatible.
Type 'UI' is not assignable to type 'UI'.
Property 'logLevel' is protected but type 'UI' is not a class derived from 'UI'. (2345)
Really appreciate you all looking into this; thank you!
@tomdale are you using Webpack, tsc or another build tool? My issue seems to only happen when compiled via Webpack (see the linked repo from my previous comment).
@seansfkelley That looks like https://github.com/TypeStrong/ts-node.