TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Duplicate type declarations with npm link

Open kimamula opened this issue 10 years ago • 147 comments

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?

kimamula avatar Jan 15 '16 03:01 kimamula

That's right, it's using ts-node (for the root application). The dependencies, however, are packages compiled with tsc.

tomdale avatar Oct 22 '16 01:10 tomdale

I just ran into this issue and it is a major problem for us because we use try to split up our back end into many small libraries. During development, we often need to npm link our repos. The specific issue I ran into which prompted me to find this is the use of rxjs Observables and interfaces:


// in repo A
export class HttpAdapter {
    request(url: string, options?: HttpRequestOptionsArgs): Observable<HttpResponse> {
        return Observable.of({});
    }
}

// in repo B
export class HttpRequestAdapter implements HttpAdapter {
    request(url: string, options?: HttpRequestOptionsArgs): Observable<HttpResponse> {
        return Observable.of({});
    }
}

This works if I don't npm link, but when I do, I get:

Error:(10, 14) TS2420:Class 'HttpRequestAdapter' incorrectly implements interface 'HttpAdapter'.
  Types of property 'request' are incompatible.
    Type '(url: string, options?: HttpRequestOptionsArgs) => Observable<HttpResponse>' is not assignable to type '(url: string, options?: HttpRequestOptionsArgs) => Observable<HttpResponse>'.
      Type 'Observable<HttpResponse>' is not assignable to type 'Observable<HttpResponse>'.
        Property 'source' is protected but type 'Observable<T>' is not a class derived from 'Observable<T>'.

jeffwhelpley avatar Oct 28 '16 03:10 jeffwhelpley

The only suggestion I can make is to avoid private. I don't publish any packages with private anymore because of this issue and just use JavaScript-style _ prefixes instead. I run into it with https://github.com/Microsoft/TypeScript/issues/7755 which is a similar discussion around why private kicks into a nominal type system instead of structural, and have hence banned it on my own projects because it's too easy to end up with version differences (E.g. NPM 2 or using npm link).

blakeembrey avatar Oct 28 '16 03:10 blakeembrey

@blakeembrey when you say avoid private, are you suggestion that I can change something in my code? I am assuming that the Observable type definition is the problem, no?

jeffwhelpley avatar Oct 28 '16 03:10 jeffwhelpley

@jeffwhelpley Yes, sorry, your not at fault. It's Observable. Unfortunately the avoid private advice is very slim and wasn't entirely applicable to you 😄 Maybe you can make an issue on, I'm assuming, rxjs about the use of private in their public interfaces?

Edit: I mostly commented because I had followed the issue earlier and avoided join with my own experiences, but figured I could write my thoughts down again too instead they're similar to https://github.com/Microsoft/TypeScript/issues/6496#issuecomment-255232592 (where @tomdale suggests eliminating private and protected, I did the same a while back).

blakeembrey avatar Oct 28 '16 04:10 blakeembrey

I got the impression from @mhegazy that he felt there was no issue with npm link. But it still seems to be plaguing us and others. So I'm not sure where this issue stands? Is it an acknowledged issue with TS 2.0+ or am I just missing a workaround somewhere?!?

xogeny avatar Oct 28 '16 14:10 xogeny

I'm getting this same issue and it doesn't appear to be caused by npm link. I still get it if I install it using npm install file.tar.gz. Here's the error:

app/app.component.ts(46,5): error TS2322: Type 'Observable<boolean | Account>' is not assignable to type 'Observable<boolean | Account>'.
  Property 'source' is protected but type 'Observable<T>' is not a class derived from 'Observable<T>'.

Here's what my app.component.ts looks like:

export class AppComponent implements OnInit {
  private user$: Observable<Account | boolean>;
  private loggedIn$: Observable<boolean>;
  private login: boolean;
  private register: boolean;

  constructor(public stormpath: Stormpath) {}

  ngOnInit() {
    this.login = true;
    this.register = false;
    this.user$ = this.stormpath.user$;
    this.loggedIn$ = this.user$.map(user => !!user);
  }

It's complaining about the this.user$ line. Stormpath has user$ defined as the following:

@Injectable()
export class Stormpath {

  user$: Observable<Account | boolean>;

mraible avatar Oct 28 '16 14:10 mraible

@xogeny Odd, my understanding was the definition identity was tied to file location which would mean they are always going to cause issues using npm link (because the npm linked dependency would have it's own dependencies installed). Perhaps definition identity has been changed - using file hashes might be a good workaround in TypeScript. Unfortunately there's just a dozen different ways to end up with duplicate modules in JavaScript (npm install from GitHub, npm install, manual clones, version conflicts can even result in the same version landing in different locations because of how node's module resolution algorithm works, etc).

blakeembrey avatar Oct 28 '16 16:10 blakeembrey

@blakeembrey Perhaps. But then what was this about?

Note, I'm not complaining. I'm just trying to figure out if there is some hope of this being resolved or not. It is a serious thorn in our side for all the reasons @jeffwhelpley mentioned.

xogeny avatar Oct 28 '16 19:10 xogeny

@xogeny I know, I'm trying too, I'd love to see it resolved correctly 😄 I read the linked issues, but it they are all designed to resolve the realpath of a symlink which implies if you have two (real) files they'll still conflict because they'll resolve to different locations. Which is what happens when you npm link from one project into another as both would have their own dependencies that can differ with re-exported symbols from the npm linked package.

Edit: I can confirm, all the issues are because of two files. npm link would trigger it because it's simple to have a dependency in a repo that you just linked that is the same dependency as in the project you linked to. A simple repro would be to do an npm install of the same dependency at two different levels of an application and watch them error out.

image

blakeembrey avatar Oct 28 '16 20:10 blakeembrey

To anybody following this thread...I tried the workaround described here and it seems to work (so far).

xogeny avatar Oct 28 '16 20:10 xogeny

I've reproduced this error.

mkdir a; cd a
npm install rxjs
echo 'import * as rx from "rxjs"; export const myObservable: rx.Observable<number>;' > index.d.ts
echo '{ "name": "a" }' > package.json

cd ..; mkdir b; cd b
npm install rxjs
npm link ../a
echo 'import * as rx from "rxjs"; import * as a from "a"; const x: rx.Observable<number> = a.myObservable;' > index.ts
tsc index.ts --target es6 --moduleResolution node

Since there are two installations of rxjs, we get:

index.ts(1,59): error TS2322: Type 'Observable<number>' is not assignable to type 'Observable<number>'.
  Property 'source' is protected but type 'Observable<T>' is not a class derived from 'Observable<T>'.

ghost avatar Nov 01 '16 18:11 ghost

i have a workaround which works great for commandline, but visual studio still gets all messed up: https://github.com/Microsoft/TypeScript/issues/11107#issuecomment-254003380

my new workaround for Windows + Visual Studio 2015 is to robocopy my xlib Library src and dist folder into the node_modules\xlib\src and node_modules\xlib\dist folders of the consuming project.

here is the significant part of my robocopy batch file script if anyone wants it:

:rerunloop
    @echo watching for changes to project files..............  (Ctrl-C to cancel)

    @rem xlib --> blib and slib
    @robocopy .\xlib\src .\blib\node_modules\xlib\src *.*  /MIR /NJH /NJS /NDL /XD .git
    @if NOT "%errorlevel%" == "0" (
        @rem copy occured, so copy both

        @robocopy .\xlib\dist .\blib\node_modules\xlib\dist *.*  /MIR /NJH /NJS /NDL /XD .git   
        @robocopy .\xlib\src .\slib\node_modules\xlib\src *.*  /MIR /NJH /NJS /NDL /XD .git     
        @robocopy .\xlib\dist .\slib\node_modules\xlib\dist *.*  /MIR /NJH /NJS /NDL /XD .git

        @rem  set the src dirs readonly
        @attrib +R .\blib\node_modules\xlib\src\*  /S /D
        @attrib +R .\slib\node_modules\xlib\src\*  /S /D
    )
    @timeout /t 1 /nobreak > NUL
@goto rerunloop

jasonswearingen avatar Nov 01 '16 20:11 jasonswearingen

Sorry for bugging again with this issue, but it is a serious drag on our project not being able to do npm link while we are making changes. I would love to help out with a PR if one of the current TypeScript contributors could give me a little guidance on where to start looking in the codebase.

jeffwhelpley avatar Nov 29 '16 17:11 jeffwhelpley

I'm also struggling with this one. We have adopted TS starting from a small app, now we have split it in submodules and linked them and… BOOM. TS won't compile anymore. Is this still an issue in all TS dist-tags? I'm currently experiencing this in @rc (2.1.1).

heruan avatar Nov 29 '16 17:11 heruan

@heruan and @jeffwhelpley can you give typescript@next a try, we have fixed a few related issues. and if you are still seeing the issue, please provide more information about your project setup.

mhegazy avatar Nov 29 '16 17:11 mhegazy

@mhegazy I am on Version 2.2.0-dev.20161129 and still having the issue. The specific issue is that I have one project (let's call it ProjectA) that contains an "interface" (using a class, but that is so I can use the class as a token for Angular 2 DI) as follows:

export class ServerAdapter {
    start(opts: ServerOptions): Observable<any> {
        return null;
    }
}

Then in a completely separate project (let's call it ProjectB) that has a class which implements the interface from the first project like this:

export class RestifyServerAdapter implements ServerAdapter {
    start(opts: ServerOptions): Observable<any> {
        let server = restify.createServer();
        this.addPreprocessors(server);
        this.addRequestHandler(server, opts);
        return this.startServer(server, opts);
    }

   // more stuff here that is not relevant to this issue
}

When I do a normal typescript compile for ProjectB it works fine. But if I npm link ProjectA from the ProjectB root directory and then run tsc again I get:

Types of property 'start' are incompatible.
    Type '(opts: ServerOptions) => Observable<any>' is not assignable to type '(opts: ServerOptions) => Observable<any>'. Two different types with this name exist, but they are unrelated.
      Type 'Observable<any>' is not assignable to type 'Observable<any>'. Two different types with this name exist, but they are unrelated.
        Property 'source' is protected but type 'Observable<T>' is not a class derived from 'Observable<T>'.

jeffwhelpley avatar Nov 29 '16 18:11 jeffwhelpley

I couldn't replicate in a mock project, I guess I'm missing the cause of the problem hence I'm unable to replicate it. @jeffwhelpley can you publish a mock project replicating the issue? I think a Lerna project should be fine and easily testable.

heruan avatar Nov 29 '16 18:11 heruan

@heruan I will try to set that up.

One, FYI, though. I think I may have found a work around. The problem is resolved if I npm link rxjs in both ProjectA and ProjectB. This sort of makes sense because in that case, both ProjectA and ProjectB are using the same exact rxjs files. Without that, they are technically using different files (even though the same version):

If you just npm link ProjectA from ProjectB, then:

  • ProjectB is pointing to node_modules/rxjs
  • ProjectA exists as a symlink in node_modules/ProjectA and the rxjs it references is at node_modules/ProjectA/node_modules/rxjs

But if you npm link rxjs in both then both of those rxjs references will be symlinked to the same exactly global npm location.

Anyways, this is obviously still not ideal, but at least something that can move us forward.

Also...not sure if this is relevant or matters (will see once I set up the test project), but my two libs (i.e. ProjectA and ProjectB) are actually private npm repos.

jeffwhelpley avatar Nov 29 '16 19:11 jeffwhelpley

Thank you @jeffwhelpley for the hint, but since I'm using Lerna all the modules are already linked to each other so they read the same file, but I think the TS compiler is taking into account the link path and not the real one. I may be wrong, since I cannot reproduce on a mock project, and I'm really driving out mad…

heruan avatar Nov 30 '16 09:11 heruan