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

import typescript into deno_std #428

Closed
ry opened this issue May 21, 2019 · 4 comments
Closed

import typescript into deno_std #428

ry opened this issue May 21, 2019 · 4 comments

Comments

@ry
Copy link
Member

ry commented May 21, 2019

Yes, it's built into deno, but it's difficult to access from the special snapshot where it's stored.

It would be nice to have access to typescript in 3rd party code. So the easy solution is to just import it in here.

@kitsonk
Copy link
Contributor

kitsonk commented Sep 13, 2019

@ry and talked about this... it makes sense... largely we need a CompilerHost that works well under Deno, which would be similar to what we use in the built in Deno compiler (possibly to become the host).

I am going to be taking a look at it, but anyone else wanting to help would be glad to talk about what needs to be done.

@ry
Copy link
Member Author

ry commented Sep 13, 2019

As a very first pass, I think just having a test that loads TS and checked the version (or some other basic thing) would be sufficient.

@kitsonk
Copy link
Contributor

kitsonk commented Sep 13, 2019

For anyone interested, the "trick" is that typescript.js loads in two ways, either as CommonJS or as global (see: microsoft/TypeScript#32949). Because it does not detect what it needs for CommonJS it creates a global variable ts when loaded.

So we need to combine the @deno_types directive and also import typescript.js and scope the type definitions in the global scope (similar to what we do in deno/deno_typescript/globals.d.ts or deno/js/ts_global.d.ts).

So something like this:

// @deno-types="./vendor/typescript.d.ts"
import "./vendor/typescript.js";

import * as ts_ from "./vendor/typescript.d.ts";

declare global {
  namespace ts {
    export = ts_;
  }
}

@kitsonk
Copy link
Contributor

kitsonk commented Sep 15, 2019

@ry I have it mostly working with the following (with the couple recent patches I made to deno master):

// @deno-types="./vendor/typescript.d.ts"
import "./vendor/typescript.js";

import * as ts_ from "./vendor/typescript.d.ts";

declare global {
    // eslint-disable-next-line @typescript-eslint/no-namespace
    namespace ts {
        // @ts-ignore
        export = ts_;
    }
}

But the problem is that we load everything as a module into the isolate at runtime, which means that the global ts variable is created only in the scope of the module. Unlike prettier, which properly detects that it isn't being loaded as a CommonJS or AMD module and defaults to exposing itself as a global variable, TypeScript only detects CommonJS otherwise just keeps itself scoped. I already opened microsoft/TypeScript#32949 which would fix this properly, but it seems like the only real choice I have is to postpend to vendor/typescript.js something that properly lifts it into the global scope.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants