phil294/coffeesense

Is it possible to get types for `.coffee` files without adding `.d.ts` files adjacent to them?

STRd6 opened this issue · 5 comments

STRd6 commented

First off, thank you for this extension! It is of great use to me overall.

In my simpler projects that have only one or two .coffee files I can easily access all the TypeScript types from imports and other shared locations. On my larger projects that have many .coffee files, some of which require others, they lose the types after the first layer.

Very basic example:

# src/jadelet.coffee
Jadelet = require "jadelet"

module.exports = Jadelet

This file works, all the types are available because the jadelet package exports the types.

Here is the problem...

# src/observable.coffee
{ Observable } = require "./jadelet"

module.exports = Observable

Cannot find module './jadelet' or its corresponding type declarations.CoffeeSense [TS](2307)

The module ./jadelet exists but TypeScript doesn't know about it. A work around is to create jadelet.d.ts files by manually compiling .coffee -> .js -> .d.ts but that seems excessive since all the data is "almost" there. Is it possible to tell TypeScript more directly that the types in these files exist?

With the JSDoc support .coffee files can pretty much export fully annotated types (especially after importing from ./typings/ etc.). Is there anything that can be done to let tsserver know that non-ts files could contain types?

I think all you need is to specify the file extension:

{ Observable } = require "./jadelet.coffee"

Autocompletion should even suggest this to you as it scans the available file paths.

Does this solve it for you?

Not sure if missing extensions can be achieved, I remotely remember that only jsx,tsx,js and ts can be implicitly imported and everything else needs explicit file extensions, maybe I am wrong though. CoffeeSense could probably also fix this

yeah custom file extensions are not allowed by typescript: microsoft/TypeScript#10939 but the extension should fix this imo. I'll fix it some other day so that your code also works nicely.

STRd6 commented

Thanks for the fast response!

Adding the extension works though I would like the flexibility of not needing to specify it. That way I could have .ts or .coffee modules and not care about migrating from one to the other. It does seem that Node is general is moving towards explicit extensions but I prefer the 2014 aesthetics as I'm sure many other CoffeeScript hold outs do as well :)

This is now possible with version 1.9.0 (you need to check that VSCode updated the extension, it usually takes two reloads for that). Can you test this out and tell me if it now works as desired?

STRd6 commented

@phil294 It works!