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

when importing d.ts file, it should have a better error message #4267

Closed
keroxp opened this issue Mar 5, 2020 · 8 comments
Closed

when importing d.ts file, it should have a better error message #4267

keroxp opened this issue Mar 5, 2020 · 8 comments

Comments

@keroxp
Copy link
Contributor

keroxp commented Mar 5, 2020

  • Deno: v0.35.0

.d.ts file can't be compiled when it is executed from CLI.

$ deno empty.d.ts
Compile file:///Users/keroxp/src/deno/empty.d.ts
No such file or directory (os error 2)

Import without declaration cause same error.

some.ts

import "./empty.d.ts"
$ deno some.ts
Compile file:///Users/keroxp/src/deno/some.ts
Compile file:///Users/keroxp/src/deno/empty.d.ts
No such file or directory (os error 2)

With declaration, no error.

import * as empty from "./empty.d.ts"
@kitsonk
Copy link
Contributor

kitsonk commented Mar 5, 2020

This is expected behaviour. There is no code to run in a .d.ts file, so telling Deno to run a .d.ts file makes no sense. A .d.ts file describes what is in a JS file, and you would need to reference the .d.ts file in an appropriate fashion: https://deno.land/std/manual.md#using-external-type-definitions

Doing a namespace import, you are referencing types in that file, bringing them into the other file and referencing them. There is still no JavaScript emit from those files, but it brings those types into the type checking of another TypeScript file, and so is valid.

What is the use case? The only "valid" use case is potentially having a .d.ts file with some global scope augmentation, which is more of trying to use the Deno compiler as a generic TypeScript compiler, in which the Deno.compile() and Deno.bundle() are better suited for that, and they support things like using lib and types. But deno run something.d.ts should never work.

@ry
Copy link
Member

ry commented Mar 6, 2020

No such file or directory (os error 2) is maybe not the right error message, but I agree with @kitsonk... what would you expect this to do?

@keroxp
Copy link
Contributor Author

keroxp commented Mar 6, 2020

I know there are few use cases for that situation. I just feel it's strange that valid typescript file can't be imported. I think running .d.ts file should show error message before compiling or emit empty js file.

I met this situation on #4197 . I tried to apply JSX namespace in jsx file by just importing .d.ts file. Eventually namespace import resolved the problem but tsc in Node.js accepts both importation for that purpose (maybe).

@ry
Copy link
Member

ry commented Mar 6, 2020

I think running .d.ts file should show error message before compiling or emit empty js file.

I agree that it needs a better error message.

Maybe you can provide some fake sample output of what you'd expect to happen and then it will be easier for us to implement it.

@ry ry changed the title d.ts file can't be imported when importing d.ts file, it should have a better error message Mar 6, 2020
@kitsonk
Copy link
Contributor

kitsonk commented Mar 6, 2020

I had an additional thought about the:

import "something.d.ts";

This is the signal to the TS compiler to not drop the module from the emit because it has runtime side effects. This is theb not removing it from the emit, which is what occurs when it is a named import (because TypeScript realises it is types only and can be dropped). Deno then comes along, being told to load a file that didn't get emitted from the compiler, so it is a cache miss.

I believe in TS 3.8 there is the a type only import, which I believe might make the use case work without using some other triple slash directive.

@lucacasonato
Copy link
Member

@bartlomieju fixed this a while ago. We just emit an empty module for d.ts files now. Duplicate of #6746 which is closed

@TradeIdeasPhilip
Copy link

This is expected behaviour. There is no code to run in a .d.ts file, so telling Deno to run a .d.ts file makes no sense. A .d.ts file describes what is in a JS file, and you would need to reference the .d.ts file in an appropriate fashion: https://deno.land/std/manual.md#using-external-type-definitions

404, Not found

(I'm looking for a good starting place for *.jsx, skimming everything!)

@kitsonk
Copy link
Contributor

kitsonk commented Nov 13, 2020

Documents move... updating comments in closed issues is usually not done. Here is the link though: https://deno.land/manual@v1.5.2/getting_started/typescript#using-external-type-definitions

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

5 participants