-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Support .d.ts files #2746
Support .d.ts files #2746
Conversation
Awesome - I'm excited about this - not being able to use the TypeScript compiler in our TypeScript runtime has been very annoying. Just a quick atheistic comment:
I actually preferred your original Shortening |
Ok, so my working branch, this now works: // @deno-types="https://unpkg.com/dayjs@1.8.15/index.d.ts"
import dayjs from "https://unpkg.com/dayjs@1.8.15/esm/index.js";
const date = dayjs();
console.log(date.seconds()); Which has an error (the API is
Fixing it then outputs:
Getting excited... The other part I need to do though is that if the value of the directive looks like a directory to resolve the |
Ok, I have tested now with lodash-es but using the types from // @deno-types="https://unpkg.com/@types/lodash@4.14.136/index.d.ts"
import * as _ from "https://unpkg.com/lodash-es@4.17.15/lodash.js";
console.log(_.add(1, 2)); The change I made is that the hint only applies to the next We still won't be able to support all type definitions at the moment, since some try to import or reference types from other type libraries. (e.g. |
Are you able to load the TS compiler with this? (Review soon...) |
That is my next use case to test. |
@ry so it works from this PRs perspective. The following: // @deno-types="https://unpkg.com/typescript@3.5.3/lib/typescript.d.ts";
import * as ts from "https://unpkg.com/typescript@3.5.3/lib/typescript.js";
console.log(ts.version); Outputs:
But the way the |
If you look at the end of https://raw.githubusercontent.com/microsoft/TypeScript/master/lib/typescript.js
not sure if this is the only export. I did manage to get typescript imported with .d.ts types after creating a rollup bundle that just reexports everything. |
@afinch7 that makes sense, and I don't know how I missed it. Currently we don't have an easy way to load stuff like this in Deno (nor do I think we should). With known things like TypeScript, it should be fairly easy to wrap it and have it be a ESM file. But in theory it is tangental to this PR. So we can now load the types, but we can't load the module. Since really TypeScript should support output to ESM, I opened issue microsoft/TypeScript#32949 to request it. |
|
||
The TypeScript compiler supports triple-slash directives, including a type | ||
reference directive. If Deno used this, it would interfere with the behavior of | ||
the TypeScript compiler. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could maybe intercept and remove a triple slash reference...
I'm still not sure about this syntax.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If we used something that conflicted with TypeScript, we could remove it for Deno, but we would want it to be a noop everywhere else, otherwise we would have Deno only TypeScript.
Even for Deno, if it overlapped TypeScript syntax, we would need a way to disambiguate it, which I don't think would be easy, so we could use a triple-slash directive, but it would have to be something TypeScript currently ignores, so the following would be out:
/// <reference path="..." />
/// <reference types="..." />
/// <reference lib="..." />
/// <reference no-default-lib="true"/>
So something like this "could" work and would be terse enough:
/// <reference deno="..." />
The biggest "challenge" I see is that these are like include statements, and they effect the file they are in. What we need to support in Deno is something where certain statements are impacted, so they are external and their position matters. Because there is such a dramatic behaviour change, it maybe confusing.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM - sorry for the delay. This is an important feature, thanks @kitsonk.
I have concerns about this syntax, but it's better to merge this and edit than risk conflicts.
Thanks. Yeah I think it is best to iterate on it, even if we change the syntax. Hard to tell what we don't like. It may also struggle with some real world use cases which we can start to address. I implemented a straight forward cache in the compiler that existed previously. I did it to help ensure once we had replaced a file we knew it. I suspect that had a side impact of reducing the threads that went back to Rust. |
Fixes #1432
I have a MVP where the following is working:
Where instead of TypeScript trying to load
foo.js
andbar.js
, it instead loadsfoo.d.ts
andbar.d.ts
. There are quite a few more scenarios that need to be supported and I need to try it out with some real world code, but it looks like it will work.