Skip to content
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

ts object has no properties when loading typescript.js in a browser with v5.5-rc #58893

Closed
1 task done
lauraharker opened this issue Jun 17, 2024 · 5 comments · Fixed by #58895
Closed
1 task done

ts object has no properties when loading typescript.js in a browser with v5.5-rc #58893

lauraharker opened this issue Jun 17, 2024 · 5 comments · Fixed by #58895
Assignees
Labels
Needs Investigation This issue needs a team member to investigate its status.

Comments

@lauraharker
Copy link

Acknowledgement

  • I acknowledge that issues using this template may be closed without further explanation at the maintainer's discretion.

Comment

This came up after sending the initial Google v5.5-beta feedback in #58685 since I was just looking at build failures for that feedback.

Apologies if this isn't an officially supported way of using the TypeScript compiler or not - most of the docs I can find are specific to running the compiler via Node.

Google has some internal web apps that load the bundled TSC from https://github.com/microsoft/TypeScript/blob/v5.5-rc/lib/typescript.js file in a browser. One example use case is a tool similar to https://www.typescriptlang.org/play/.

In TS 5.5 these apps break at runtime because they call methods on the global ts object like ts.createLanguageService, but ts is now just an empty object with no properties. I narrowed this down to the bundling changes in #57133. Here's typescript.js before & after:

v5.4.2:

var ts = (() => {
  // lots and lots of code
  return require_typescript();
})();

if (typeof module !== "undefined" && module.exports) { module.exports = ts; }

v5.5-rc:

var ts = {}; ((module) => {
// lots and lots of code
module.exports = __toCommonJS(typescript_exports);
// even more code
})(typeof module !== "undefined" && module.exports ? module : { exports: ts });
if (typeof module !== "undefined" && module.exports) { ts = module.exports; }

I'd assume that in v5.5-rc, ts was intended to be instantiated via passing {exports : ts } as the module argument to this IIFE, causing the exported functions that are put on module.exports to also be defined as properties on ts. Instead the module.exports = __toCommonJS(typescript_exports); assignment just overwrites ts in {exports: ts} and ts is just an empty object.

@RyanCavanaugh RyanCavanaugh added the Needs Investigation This issue needs a team member to investigate its status. label Jun 17, 2024
@jakebailey
Copy link
Member

Ugh, I thought this was tested.

Apparently our browser test only tests that the code executes, not that it actually declares a working global. Will fix this ASAP.

@jakebailey
Copy link
Member

Note that I wouldn't 100% consider this supported; it's possible that this loading method may go away in a PR like #58419. But, there's not one form that works for both without going through a bundler or esm.sh or something.

@lauraharker
Copy link
Author

Thanks for the quick fix & additional context @jakebailey! We can certainly switch to loading tsc as an ES module in browsers if/when that lands

@fatcerberus
Copy link

@hossain666 Please stop posting spammy comments everywhere

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Needs Investigation This issue needs a team member to investigate its status.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants