-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
No longer possible to use TS API globally in types (5.0.0) #53402
Comments
TypeScript switched to ECMAScript modules in 5.0. You might be interested in this: https://github.com/microsoft/TypeScript/wiki/API-Breaking-Changes#typescript-50 |
I thought it should affect runtime only. This issue is about types only. Anyway, I have only one question in this issue: how to use types of module |
Thanks for reporting this; this is something I should have documented as a change to the package. The runtime didn't change, but yeah, the types no longer plop themselves onto the globals. I'll need to see if there's a way to bring that back or not. Probably because I dropped What you should be able to do as workaround is to write this in a d.ts file somewhere: import tsModule = require("typescript");
declare global {
var ts: typeof tsModule;
} Or: import tsModule = require("typescript/lib/tsserverlibrary");
declare global {
var ts: typeof tsModule;
}
Out of curiosity, what are you trying to do where importing is undesirable? I'm confused by your example; |
If what you're doing is this: https://github.com/zardoy/typescript-vscode-plugins/blob/9641211286cc0764fa503f86a105082a5feff08e/typescript/src/definitions.ts Then yeah, this doesn't seem like a good idea at all... Why not import it like everything else? This "global" behavior is only applicable to the web. |
I would strongly suggest an approach more like: https://github.com/mrmckeb/typescript-plugin-css-modules/pull/213/files |
This is the biggest plugin we have, I also noticed this behavior in of my private plugins, but yeah, the same issue lays here.
I already thought about adding btw @talks2much in mentioned file we have
To be honest, not sure you should bring old behavior when type is declared in global scope implicitly. I think if we found workaround we wouldn't raise this issue. Now I wonder if this is a TypeScript limitation?
As said in issue body, we already tried this workaround, it declared only variable globally. @talks2much do you have any additional ideas? |
Also thanks for opening this, didn't have much time to prioritize it :) I personally also just tried this: // ts.d.ts
export * from 'typescript'
export as namespace ts; But it gives the following error:
(which doesn't seem to be logical for d.ts) |
What about: import ts = require("typescript");
export = ts;
export as namespace ts; |
But, if you are a language service plugin, you definitely want |
You could also just |
Exactly, I suppose it's just a mistake in the body of this issue. Also wanted to answer this:
In this plugin I have tricky way of using
Aaand it works! I just also added This seems to be the only way to declare module type globally. This is exactly what we have been looking for for a long time. |
Indeed, but I didn't know there is a difference between
Hm, probably you're right. The workaround look insanely good. Though why umd-specific feature is the only way to do it? This seems to be illogical to me: // I can do this
type LS = import('typescript').LanguageService
// but, this is an error
type ts = import('typescript') // Module 'typescript' does not refer to a type, but is used as a type here. Did you mean 'typeof import('typescript')'?
type LS = ts.LanguageService @jakebailey am I right that this is a TypeScript limitation there? E.g. why module 'typescript' can't refer to a type? |
You can do |
We need to upgrade TypeScript to land the following fixes: - #50289 - #48018 And it should fix async components: - https://twitter.com/sebsilbermann/status/1664356039876124672 Also see this related TS 5.0 issue: - microsoft/TypeScript#53402
@jakebailey We were using your pattern from #53402 (comment). and it was brilliant, worked perfectly! It still worked when we were using TS 5.5 beta and RC, but broke after official release 5.0: //ts.d.ts
// eslint-disable-next-line @typescript-eslint/no-require-imports
import ts = require('typescript')
export = ts
export as namespace ts
declare global {
const ts: typeof import('typescript')
}
If I remove So as I understand now its now only possible to either use the type or variable with the same name globally, but not both? |
I'm not sure that using both I'm also unsure what you mean about "using 5.5 beta and RC but broke after 5.0"; do you mean after 5.5's full release? Consider bisecting with https://www.npmjs.com/package/every-ts if so. |
Oh yes, I apologize for the confusion, I meant the 5.5 release. Thanks helping! And yes, having both global declaration and global type definition e.g. so I could use |
I would guess that #58326 is what changed this, but that was in the RC... In any case, not 100% clear why you'd both declare a global explicitly, but then also have |
This allows me to use the type globally (as you suggested above). Having just
My bad, you are right, it was already broken in rc but was ok only during the beta. |
Oh so they were aware of that breakage 😥 |
Bug Report
Hi! We have TypeScript plugin and after recent update to latest TypeScript version to
5.0.2
we no longer can usets
in types in our files e.g.:We don't like to rewrite everything to use imports (
import('typescript').LanguageService
) and we more like the current state of codebase with concise type declarations🔎 Search Terms
Type import module use globally, define type module globally, declare global type
We tried this workaround:
But in other.ts:
But I personally have no idea why it doesn't capture module's type into declared type
//cc @zardoy
The text was updated successfully, but these errors were encountered: