Better handling of untyped node module imports #11106

Closed
bterlson opened this Issue Sep 23, 2016 · 8 comments

Projects

None yet

7 participants

@bterlson
Member

Consider importing an untyped module, eg. exceljs:

import exceljs = require('exceljs');

This results in an error that the module exceljs is not found. There is at least two ways to fix this:

  1. Setting allowJs: true and maxNodeModulesJsDepth: 1
  2. Declare an ambient module in a separate file

I think it would be better if instead we update module resolution semantics such that when we find an appropriate package.json without a typings entry, we store it away. Then if module resolution fails (ie. we also can't find an @types/exceljs), we go back and import the untyped package.json as a JS import. Thoughts?

You might argue that this should only be allowed with --allowJs, but it might also make sense to just always use this behavior when using node resolution rules as you may still not want to allow JS in your own project while still allowing your dependencies to be authored in JS without typings.

@mhegazy mhegazy added this to the TypeScript 2.1 milestone Sep 23, 2016
@andy-ms andy-ms was assigned by mhegazy Sep 23, 2016
@aluanhaddad
Contributor

Is there anyway we can solve this in a non npm specific manner? I like the concept but tying it to node_modules/import_name and node_modules/@types/import_name is not something I would like to see.

@patrickhousley patrickhousley referenced this issue in infinitered/reactotron Sep 24, 2016
Closed

TS2 import module not found #212

@dmitriid

Also, please, please, please improve documentation on "ambient modules". I've read the section seven times now, I still have no idea what the ambient modules are, how to define them and where to put the specs so that they are picked up by TS. Espec

It would be awesome, if there was a small section "you want to import module X. you create a file X.d.ts here. Then you do this and that and this"

@andy-ms andy-ms closed this in #11889 Oct 27, 2016
@mhegazy mhegazy added the Fixed label Oct 27, 2016
@reverie
reverie commented Nov 16, 2016

I followed a chain of four closed tickets to get here. Glad to see the issue was addressed. Could someone please point to docs for this feature? How do I use it? For my sake, and for everyone else who may end up here. :)

@mhegazy
Contributor
mhegazy commented Nov 16, 2016

This is part of typescript@2.1.3 release, so for now you will need npm install typescript@next.

Now having an import like import foo from "Foo" the compiler will check if there is a package Foo, e.g. node_modules\Foo\index.js if one is available, even if it does not have a .d.ts file along with it, no error will be reported. the type of the imports will be any.

if you use --noImplicitAny, you would get these import flagged as implicit anys.

hope that helps.

@reverie
reverie commented Nov 16, 2016

That helps a lot, thank you. It seems like for now (v2.0.10) we can also use declare module "Foo";?

@mhegazy
Contributor
mhegazy commented Nov 16, 2016
@radicaled

Now having an import like import foo from "Foo" the compiler will check if there is a package Foo, e.g. node_modules\Foo\index.js if one is available, even if it does not have a .d.ts file along with it, no error will be reported. the type of the imports will be any.

Does this mean that if you've got a node module that doesn't use index.js but instead relies on a main field in package.json that this feature doesn't work?

@andy-ms
Collaborator
andy-ms commented Dec 3, 2016

@radicaled We check for that too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment