-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
importing type declaration files #16472
Comments
just import the package itself, and not the |
The problem I have with that is it looks like you're actually importing the package, when you're just using the typings. I think there should be a way for you to show that you're explicitly importing typings, without forcing a dev to go look in the |
you are importing the package. types are just illusion at design time. they do not exist at run time.
use |
I know this, which is why I don't like the fact that it looks exactly like a normal import. It's code that isn't emitted, so shouldn't it be more recognisable as such?
That's worse though as it means you then have seemingly undefined variables in your code. (see the AWS example in my first comment). Both of these things make it easy to trip not only new developers up, but also trip yourself up. |
does it represent code that exists at runtime? if so, do not see the issue, if not, then why not model the run-time behavior more accurately.
I am not sure i understand this. why would this be confusing? and why would |
But relying on ambient defs means you can have a bunch of namespace that just exist in the code, with no clear way to discern what def they are from.
It doesn't always represent code that exists at runtime, see the AWS example. The variables in the handler exist at runtime, but the types certainly do not. The problem is that now there's no way to signify to a dev "hey these types exist solely for typings sake, there's no library involved, but here's where these namespace are coming from for future reference" |
but is not that the case for all types.. why is |
that's exactly my point.
But This is even more confusing in say the case of Where as Both of the statements will not be emitted at compile time, but one is clear and predictable. This may all seem like a problem solely for that typings package, but there would be other examples where you may solely want to import types, and want that to be clear. |
looks like you are looking for something like #2812. we have decided to no differentiate between imports in type and value space. and in 99% of the time the two are the same thing.
or, you typed that thinking it gets you the actual import. as a matter of fact we allowed this in the past, then restricted it based on feedback. |
@mhegazy What's the recommended path going forward then? Should we replace all imports to |
In the case of typings for I didn't want to use the real fake import (partially above, partially because it goes against our lining standards), so I just went with using the ambient typing. |
Thanks for the info @bradzacher!! 👍 |
It sounds like your linting standards need to be updated since they are encouraging you to use global variables. |
The linting standards flag importing of packages not listed as a dependency in Because we are using the package Considering that the npm package As detailed in the thread above. If I could, I'd prefer to stick to importing the clearly defined |
In the vast majority of cases, an import from a module specifier beginning with |
…orting 'express' instead of '@types/express' for more info see microsoft/TypeScript#16472
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
I updated to 2.3.4 -> 2.4.0 and now I'm unable to do
import * as lib from '@types/lib'
, as it throws an error:error TS6137: Cannot import type declaration files
.I thought that this was a great way to programatically include type definitions into a clearly defined namespace, which then gets scrubbed at compile time (as opposed to /// referencing them).
I can understand blocking the direct importing of def files in ambiguous contexts to help prevent runtime errors (i.e. if you
import * as lib from 'lib'
, then you should expect it to only import a non-def file), but I feel that doing an import of something in the@types
package namespace, or a file ending with.d.ts
should be allowed, as it's explicitly importing typings.It helps to work around things where you actually want types but don't use a library.
For example, with AWS lambda:
It's clear that you're not using an actual aws-lambda library, just the types.
It's also clear where the
lambda
namespace came from, so new developers to the code don't wonder where random namespaces might come from, which is what happens when you rely solely on the ambient type definition:It helps to work around problems like defining types to library A which is designed to fit the same API as library B.
i.e. the
mysql2
package is designed to fit mostly the same API as themysql
package, so you can cover the majority of usage by just defining this typescript def file:again, as you've not installed the mysql package, it's clear where the types are coming from, (rather than doing
export * from 'mysql'
.The text was updated successfully, but these errors were encountered: