question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow local .js/.ts files to be typed by their corresponding .d.ts file

See original GitHub issue

Suggestion

If I have source/index.js which has:

function isObject(value) {
	// null is object, hence the extra check
	return value !== null && typeof value === 'object'
}

And then I supply a source/index.d.ts file which has:

/** Checks to see if a value is an object */
export function isObject(value: any): boolean

And I update package.json:types to source/index.d.ts.

Then I should not receive the following error in source/index.js:

[ts] Parameter 'value' implicitly has an 'any' type. [7006]
(parameter) value: any

And I should get complete intellisense.

Use Cases

This approach has the following benefits:

  1. Source files can run without compilation, this has been incredibly useful for @bevry’s hundreds of packages, which are all written in jsdoc’d esnext that runs on mostly node 4 and above, with compilation only for older node versions (but most of the time, it is our source code that is running, thanks to editions)
  2. JSDoc types do not provide intellisense nor type checking for typescript consumers
  3. TypeDoc for documentation seems nicer than JSDoc, especially with overloads
  4. Going all in on TypeScript may be too much to ask for, for many module authors - as a typescript project requires types for all its dependencies, as well as accurate (rather than merely simplistic) mappings, which some of the time, is just too inconvenient to bother with - such as this, as well as eachr which a pure typescript conversion seems impossible to me (after days of effort, as typescript always whines about incorrect types)

Alternatives to this would be:

  1. Have typescript projects support intellisense and type checking for jsdoc js files
  2. Automatically generate .d.ts files for jsdoc js files

For now, the best workaround seems to be:

  1. Spend weeks upon weeks converting your hundreds of modules to typescript to satisfy typescript’s need for types for everything
  2. Provide jsdoc .js files, and manual .d.ts files

Examples

Checklist

My suggestion meets these guidelines:

  • This wouldn’t be a breaking change in existing TypeScript/JavaScript code
  • This wouldn’t change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn’t a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript’s Design Goals.

I haven’t filled out this checklist as I do not know enough about typescript’s inner workings to be able to fill it out.

Issue Analytics

  • State:open
  • Created 5 years ago
  • Reactions:7
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
baluptoncommented, Dec 18, 2018

You mean when you publish to npm?

Yes.

Take the package https://github.com/bevry/badges and the example consumption at https://github.com/balupton/typescript-consume-jsdoc

If I consume it via a .js file, I get intellisense with the typing and docs provided by its jsdoc comments:

screen shot 2018-12-18 at 11 05 37 am

However, if I do it via a .ts file, I get nothing:

screen shot 2018-12-18 at 11 05 55 am

Only an error whining that it needs a declaration file:

Could not find a declaration file for module 'badges'. '/Users/balupton/Projects/active/typescript-consume-jsdoc/node_modules/badges/index.js' implicitly has an 'any' type.
	Try`npm install @types/badges` if it exists or add a new declaration(.d.ts) file containing`declare module 'badges';`[7016]

Which is infuriating, especially as intellisense and type checking work fine for .js files with @ts-check:

screen shot 2018-12-18 at 11 08 52 am

Which means that .ts files are actually more crippled than their .js alternatives:

feature .ts files .js files
intellisense for jsdoc’d packages no yes
type checking for jsdoc’d packages no yes
intellisense for typescript packages yes yes
type checking for typescript packages yes yes
intellisense provided by a corresponding local .d.ts file no no
type checking provided by a corresponding local .d.ts file no no

I cannot figure out any technical reason why this should be the case, only a business reason, of typescript adoption requiring the conversion of all dependencies, to do a marketshare stronghold.

This is why I ended up on the path of attempting to document .js files via corresponding local .d.ts file, as I was thinking, well typescript can’t consume jsdocs, in which case, perhaps then I can work around this by writing .d.ts files for my .js files, however then I run into the first post’s issue, which is that javascript can’t get its type information from local .d.ts files. Meaning that either:

  1. I have to convert everything to .ts files, enduring the compilation/adoption/conversion costs
  2. I have to write packages that provide both .js files with jsdoc, and a corresponding.d.ts file
    1. jsdoc is necessary for local type checking and intellisense
    2. .d.ts file is necessary for consumed type checking and intellisense in typescript projects
0reactions
frank-dspeedcommented, May 3, 2022

If i understood that right this issue votes for making the d.ts file authoritative and i think that is not fully wrong in all scenarios it depends on if your a consumer or author. So i guess this issue should get reopned and renamed to something like

tsconfig.json setting types lookup priority there are cases where you want your .ts and .js to be authoritative to create the correct.d.ts and there are cases where your the consumer want to write something with the help of a d.ts file.

that is a chicken egg problem i guess which comes first. the declaration or the definition.

at present i have a combo of declaration false and including the d.ts file as source file in my head that could lead to the expected result without the error that the emitting of files will overwrite a source file. So allowJs CheckJs include d.ts noEmit should enable that

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Creating .d.ts Files from .js files
Run the TypeScript compiler to generate the corresponding d.ts files for JS files; (optional) Edit your package.json to reference the types. Adding TypeScript....
Read more >
About "*.d.ts" in TypeScript
The "d.ts" file is used to provide typescript type information about an API that's written in JavaScript. The idea is that you're using ......
Read more >
What Are *.d.ts files? How to Use *.d.ts Files in TypeScript?
How to use *. d. ts files.
Read more >
Better mapping between *.js and *.d.ts files : WEB-47153
The only typescript file is the @types file. Now the problem. I have the src/index.js file and the corresponding src/index.d.ts. Then ...
Read more >
Announcing TypeScript 4.7
The type field in package.json is nice because it allows us to continue using the .ts and .js file extensions which can be...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found