Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

DefinitelyTyped 2.0 #738

Open
blakeembrey opened this issue Dec 12, 2016 · 17 comments
Open

DefinitelyTyped 2.0 #738

blakeembrey opened this issue Dec 12, 2016 · 17 comments

Comments

@blakeembrey
Copy link
Member

blakeembrey commented Dec 12, 2016

Cross-post of DefinitelyTyped/DefinitelyTyped#13280. What this means for Typings users:

  1. Dependencies turn into external modules, but Typings won't be able to resolve any of the module dependencies as the dependency system on DefinitelyTyped uses <reference> tags. Effectively, if you're using Typings, you may not need --global in some places anymore.
  2. You will probably need to jiggle with tsconfig.json typeRoots to get <reference type> instances to work properly (e.g. typeRoots: [typings/global]).
  3. Typings can not install anything using export as namespace as it requires native compiler support to resolve modules - while Typings uses a legacy workaround of declaring global modules for you.

There will be no disruption of existing modules using Typings as everything is keyed to a commit. If you're a new user or don't understand the implications, it's probably best to use @types on NPM and wait for microsoft/types-publisher#4.

@johnnyreilly
Copy link
Member

Thanks for clarifying!

@jcristovao
Copy link

I have a problem which I believe is related to this...

https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/js-yaml

These install only as a module, but looking at their code it seems they are designed as global, at least that's what I take from looking into them and from typescript error message:

typings/modules/js-yaml/index.d.ts(87,1): error TS1316: Global module exports may only appear at top level.

So, my question is: is this a DT bug, where those definitions should have been updated, or a typings limitation? Is there a way to force a module to install as global?

@mpochron
Copy link

@jcristovao
try to use command
typings install js-yaml=github:DefinitelyTyped/DefinitelyTyped/js-yaml/index.d.ts#a5f4d50cccf025b6a8088093e76da2b20c1220f3 --global --save
it worked for me.

@jcristovao
Copy link

Yeah, I had a fork of DefinitelyTyped and solved the immediate problem (thanks!), but I am still interested on how I should proceed in cases like this in a general way - besides grabbing old versions...

@blakeembrey
Copy link
Member Author

Sadly export as namespace is something Typings won't be able to handle - I'll add that to the OP. It requires native TypeScript integration as the compiler needs to be able to understand that a file can be both global and external depending on usage. It may have been possible to rewrite these statements, but the compiler won't allow import statements inside a normal namespace and if we write import at the top-level, it all turns into an external module which won't work either.

@jcristovao
Copy link

Ok, thanks for the clarification!

Now, I understand this is more a question for DT than here, but perhaps you can clarify - was the intend of that 2.0 branch to move every typing definition to external modules? Does it make sense / is it encouraged to raise issues or PRs there for it?

@blakeembrey
Copy link
Member Author

was the intend of that 2.0 branch to move every typing definition to external modules?

Yes, definitely. Just as Typings was written because external modules are the obvious choice when dealing with the dependency problem (to avoid conflicts), TypeScript uses the same pattern. Unfortunately, when Typings was written there was no precedent or traction for the change, but now with native TypeScript support things will continue to move to external modules for all the original reasons Typings used them.

Does it make sense / is it encouraged to raise issues or PRs there for it?

To move a module to an external module? Yes, definitely do! I'm really hoping we'll land microsoft/types-publisher#4 in the near future so the Typings registry can start syncing to DefinitelyTyped.

@bchenSyd
Copy link

https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/js-yaml

These install only as a module,

why is it installed as a module? it doesnt have export at the top

@unional
Copy link
Member

unional commented Dec 19, 2016

It does. At the bottom. 🌷

@bchenSyd
Copy link

To move a module to an external module? Yes, definitely do!

most libraries today are defined in umd fashion, which means they can be used both globally and via import. I think TypeScript 2.0 has built in support for this via export namespace as , which has already be adopted by many DefinitelyTyped/types-2.0 type definitions....
it's my understanding. correct me if i'm wrong as today is my 2nd day using typescript ;-)

@bchenSyd
Copy link

bchenSyd commented Dec 19, 2016

@blakeembrey @unional Shell I ask the author of
graphql type declaration to rewrite using external modules?


I checked it up again and it seems to me a valid external module written in the new types-2.0 syntax.
can someone help to confirm?

@unional
Copy link
Member

unional commented Dec 19, 2016

TypeScript 2.0 has built in support for this via export namespace as

Yes, correct. Unfortunately, due to how export as namespace is done, we cannot support that in typings. It is a design limitation on TypeScript side.

If you want to use the library in script tag, and use typings through typings, a global typings file need to be created and register under typings/registry/global.

@unional
Copy link
Member

unional commented Dec 19, 2016

Currently, IMO, there are a few cases you still need to use typings for type acquisition. One being that if the library's typings rely on typings of its dependencies and those dependencies does not have typings available in @types/* or distribute their typings inside the package.

For example,

Lib1 depends on Lib2 and Lib1 re-export or expose some typings from Lib2. But Lib2 does not distribute their own typings and no @types/Lib2 exist. It resides within typings/registry.

@mboudreau
Copy link

mboudreau commented Dec 21, 2016

Can someone please help? I'm hitting the issue of export as namespace with the 'Q' library on DT. Seems like typings is wrapping it in a module, causing tsc to error. I can't seem to be able to make it global since typings is not allowing it.

In the meantime, I've forked the repo and removed that line in my fork, and using it as my definition which works fine for now.

@rjrudman
Copy link

rjrudman commented Dec 28, 2016

I'm running into the same issue as jcristovao - however, the issue is for dt~react, which I cannot resolve by installing globally.

In particular, it fails on the following line in the typings file:

export as namespace React;

With the error:

error TS1316: Global module exports may only appear at top level.

I don't completely follow what the change is to exporting and why typings cannot support export as namespace.

The files are correctly compiled, though the error is generated. Removing the line works fine (note this also occurs in react-dom). Is there any issue with removing the lines?

@unional
Copy link
Member

unional commented Dec 28, 2016

If I understand correctly,

Typings with export as namespace cannot be handled by typings. This is a limitation because typings converts a module typings to a global typing by wrapping the content in declare module 'X' { ... }.

export as namespace has to be top level.
For those typings I would recommend to use npm install @types/* instead.

@rjrudman
Copy link

@unional That worked perfectly - thank you very much! I didn't know those packages existed

rjz added a commit to rjz/typescript-react-redux that referenced this issue Jan 2, 2017
  - Bumps all NPM dependencies
  - Bumps typings files where possible
  - Moves typings to direct includes where needed (typings/typings#738)
  - Updates (now-generic) `React.SyntheticEvent`
rjz added a commit to rjz/typescript-react-redux that referenced this issue Jan 2, 2017
  - Bumps all NPM dependencies
  - Bumps typings files where possible
  - Moves typings to direct includes where needed (typings/typings#738)
  - Updates (now-generic) `React.SyntheticEvent`
mcsagittarius pushed a commit to impact-as/boilerplate that referenced this issue Jan 18, 2017
sandikbarr pushed a commit to PRX/styleguide.prx.org that referenced this issue Mar 2, 2017
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

8 participants