TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

New APIs added to lib.d.ts may break client codes. Allow duplicated members in interfaces? Make lib.d.ts overridable?

Open duanyao opened this issue 11 years ago • 42 comments

I noticed that recently a lot of new APIs are added to lib.d.ts. Of cause this is a good thing, however my code breaks badly because of such update.

Previously I had added a lot of declarations in my code for HTML5 APIs that were missing in lib.d.ts . Now some of them are added to lib.d.ts, and I get quite a few "duplicated identifier" errors. For example, I had added this piece to play with fullscreen:

interface Document {
//...
  fullscreenEnabled: boolean;
  mozFullScreenEnabled: boolean;
  webkitFullscreenEnabled: boolean;
}

Now fullscreenEnabled and webkitFullscreenEnabled are available in lib.d.ts, and my code breaks, and I have to remove them. However, you are still missing mozFullScreenEnabled right? -- well, waiting for the next break.

Web platforms are constantly evolving, it is not practical to expect lib.d.ts always up to date, so developers just have to write such compensating declarations from time to time. Can TS avoid such breaks? I have a few ideas:

  • Allow duplicated members in multiple interface declarations, as long as they are identical. In the fullscreen API example above, tsc should not fire errors. Optional compiling warnings are acceptable, to assist developers to remove redundant codes at convenient time.
  • Make lib.d.ts overridable. When tsc find conflictions between lib.d.ts and client codes, it should pickup declarations in client codes. This is because declarations lib.d.ts may contain bugs, developers should be allowed to workaround them. E.g. MutationObserver's constructor was missing a parameter for a long time. Again, optional warnings are welcomed.

What do you think?

duanyao avatar May 19 '15 11:05 duanyao

For those with the Promise issue, one work-around is to use Typings instead of TSD. Typings will not install referenced type definitions (see here), and you can manually install the "dependencies" that you need (excluding the unwanted definitions of Promise).

Not ideal, but there aren't many other options at the moment.

glen-84 avatar Dec 29 '15 17:12 glen-84

+1 here as well.

I have bluebird and mssql which are both grabbing the Promise issue. For now, I have to manually approve Codeship builds since I'm not sure what's compiler noise and what's an actual error.

lindo-jmm avatar Dec 29 '15 21:12 lindo-jmm

+1

ryasmi avatar Dec 30 '15 00:12 ryasmi

I'm not sure I fully understand the issue here, but isn't the real problem that the definition files aren't using "ghost modules" hence "polluting the global namespace with many interfaces"?

ryasmi avatar Dec 30 '15 17:12 ryasmi

@ryansmith94 I'm brand new to Typescript, but it seems like the definition file is shimming by using es6-promise even though we have Promise built in now (I'm targeting ES6).

lindo-jmm avatar Dec 30 '15 17:12 lindo-jmm

Possibly @lindo-jmm, but I have the following error because of Bluebird which isn't a shim to my knowledge.

typings/bluebird/bluebird.d.ts(19,15): error TS2300: Duplicate identifier 'Promise'.
typings/bluebird/bluebird.d.ts(19,37): error TS2503: Cannot find namespace 'Promise'.

ryasmi avatar Dec 30 '15 18:12 ryasmi

+1

We have a special typing for Webpack's require function and with Angular2 (beta) node.d.ts (recently updated typings) we're now getting TS2403.... errors - the open-ended suggestion by @kitsonk worked out fine but looks a bit hacky and would make IDE confused (too many intellisense suggestions)

rixrix avatar Jan 05 '16 11:01 rixrix

+1 (This is especially a pain in Angular2)

craiglathrop218 avatar Jan 12 '16 00:01 craiglathrop218

suggested workaround for this? https://github.com/DefinitelyTyped/DefinitelyTyped/issues/5015#issuecomment-172297350 maybe?

nathantsoi avatar Jan 19 '16 06:01 nathantsoi

As one of the possible workarounds it it OK but it does not really solve the big issue...

ENikS avatar Jan 19 '16 17:01 ENikS

A massive +1, you import angular2 into a project, and BOOM! 1221 Error Lines of TS2300: Duplicate identifier due to node.d.ts and es6-shim.d.ts

As we start working with more and more modules in TypeScript this is enviable. I actually think it's good practice for each module to have all the typings that it works with, but I would propose Cascading Overwriting to solve the issue of duplicated typings.

Priority from bottom to top, : 0. typings in typescript

  1. typings in module tsd.json file
  2. typings found in module source code in cascasting order
  3. typings in original module tsd.json file
  4. typings found in original module source code in cascasting order
  5. typings in project root tsd.json file
  6. typings found in project source code in cascasting order

Examples: es6-shim.d.ts in typescript has priority 0 es6-shim.d.ts in angular2 has priority 1 es6-shim.d.ts in es6-shim module has priority 3 es6-shim.d.ts in project directory has priority 5

du5rte avatar Jan 21 '16 19:01 du5rte

@monteirocode I would not like such overwriting, it likely goes against TypeScript principles of predictability and obviousness, thus you have strong typing but don't know where the definitions came from without an extra exploration.

Objectively, I'm for ignorance of identical definitions at the first place (https://github.com/Microsoft/TypeScript/issues/3215#issuecomment-134719471). It just fixes a bunch of existing issues for many projects and doesn't create new problems or breaking changes (does it?). I would be glad to see this implemented ASAP. IMHO, only after that any extended behavior (type unions, overwriting, etc.) may be discussed.

hesalx avatar Jan 26 '16 19:01 hesalx

Hi there, I've made a change in Angular 2 to fix the BOOM! of Duplicate identifier. Will be available in beta.4. See https://github.com/angular/angular/issues/5807

I think it's a burden for users to be careful that each typing is only installed once (eg. installing es6-shim.d.ts and es6-promise.d.ts) so I hope there will still be a resolution on this issue.

alexeagle avatar Feb 05 '16 20:02 alexeagle

I have another use case:

There is a third party library that has started creating their own type definitions - unfortunately many of the interface members are just stubbed with type any, which is nearly useless.

Even with the proposal here, I cannot override the provided type definition with the type I know to be correct. The type definition itself can be updated, but in the next version you'll have to start over.

mykohsu avatar Feb 24 '16 02:02 mykohsu

Reference Type directives are designed to solve exactly this problem by providing a canonical lookup location for things which mess with the global scope.

Combined with the library decoupling work, I think we're addressing all the scenarios here as best as is feasible. There will need to be some level of restructuring of code that found itself in this state, but fundamentally it is solving the problem where two people both want to declare something in the global scope.

RyanCavanaugh avatar Apr 11 '16 20:04 RyanCavanaugh

@RyanCavanaugh Do you mean Library include directives? It is a very useful feature, but I'm not sure how "duplicated identifer" is resolved. Is it still an error to declare an identical member in a global interface in client code?

duanyao avatar Apr 12 '16 03:04 duanyao

It is a very useful feature, but I'm not sure how "duplicated identifer" is resolved

It doesn't. But it allows library declaration authors to not pollute the global namespace. Thus decreases the likelihood of global namespace collision :rose:

basarat avatar Apr 12 '16 03:04 basarat

It solves it because the offending declaration files can be rewritten to be included via library directives, which will only happen once

RyanCavanaugh avatar Apr 12 '16 03:04 RyanCavanaugh

I want to use some DOM4 APIs and installed dom4 typings (typings install -A dom4). What will happen when some DOM4 APIs are added to lib.d.ts in future? E.g.

interface ParentNode {
    children: HTMLCollection;
}

I also note that duplicate method in interface is not an error, but duplicate property is. E.g.

interface Element {    
    innerHTML: string; // error: duplicate identifer
    getAttribute(name?: string): string; // OK
    getAttribute: (name?: string) => string; // error: duplicate identifer
}

This is a strange divergence to me.

duanyao avatar Apr 12 '16 05:04 duanyao

facing same issue in beta17

manish153 avatar Jun 19 '16 00:06 manish153