TypeScript icon indicating copy to clipboard operation
TypeScript copied to clipboard

Unable to type nested fields in a Node.js module.

Open akwotom opened this issue 1 year ago • 2 comments

Does this issue occur when all extensions are disabled?: Yes

  • VS Code Version: 1.88.1
  • OS Version: Ubuntu 22.04

Steps to Reproduce:

  1. Setup a node.js project
  2. npm install tranzak-node
  3. Write the following code.
import TRANZAK from 'tranzak-node'
const client = new TRANZAK(/* Just leave the credentials empty */)

// Now, we expect auto complete with the nested fields
const sendFxn = client.sms.send; // VS code doesn't think this is a function. It thinks it is 'any'
// However, the function is actually defined in the module.
// If you Ctrl+Click, on 'sms', you would see the definition of the 'send' function.

Within the node module itself (e.g in index.mjs), you would find that the code is correctly completed.

akwotom avatar May 02 '24 05:05 akwotom

Their typings files are importing js files:

Image

TS team can weigh in but seems like an issue on the package side to me

mjbvz avatar May 02 '24 20:05 mjbvz

ATTW says this is misconfigured: https://arethetypeswrong.github.io/?p=tranzak-node%403.1.3

RyanCavanaugh avatar May 02 '24 21:05 RyanCavanaugh

ATTW says this is misconfigured: https://arethetypeswrong.github.io/?p=tranzak-node%403.1.3

Thanks for the insight. I didn't even know such a tool existed.

akwotom avatar May 03 '24 04:05 akwotom

ATTW says this is misconfigured: https://arethetypeswrong.github.io/?p=tranzak-node%403.1.3

Here's a screenshot from ATTW

Screenshot from 2024-05-03 05-53-12

It says CJSOnlyExportsDefault, which according to my intuition means, only commonjs exports have been done by the package.

However, the actual package.json has configuration for modulejs (mjs), and types

Screenshot from 2024-05-03 06-00-00

And in the index.d.ts, it simply imports the main class of the package.

Screenshot from 2024-05-03 06-04-17

At this point, I expect VS code to correctly infer the type. And yes, it does.

The problem is, nested fields are not working.

akwotom avatar May 03 '24 05:05 akwotom

ATTW says this is misconfigured: https://arethetypeswrong.github.io/?p=tranzak-node%403.1.3

Screenshot from 2024-05-03 06-13-26

Clearly, the error isn't even about types.

akwotom avatar May 03 '24 05:05 akwotom

The layout of this package is nonsensical; they're importing from index.mjs (the extension is fine and even required here!), but there is no index.d.mts that describes it. I have no idea how this could have been working before.

jakebailey avatar May 06 '24 23:05 jakebailey

The package has index.d.ts, but no index.d.mts. And, locally, I just tried renaming all .d.ts files to .d.mts, and changing the reference in package.json, but the situation didn't improve.

akwotom avatar May 07 '24 02:05 akwotom

That really isn't the problem; the problem is that there really are not any type definitions for this class. Renaming things won't help when there's no type info at at all. This is most definitely a problem with the package.

jakebailey avatar May 07 '24 04:05 jakebailey

327650413-7204a1d7-1d63-4789-8dbd-2ae37d4f4db7

VS code has JS-TS interoperability, where you can import a JavaScript class, and it would be typed correctly. And when you look at the index.d.ts file, you see how TRANZAK is imported from index.mjs The expected behaviour, is that VS code would perform it's regular type inference.

akwotom avatar May 07 '24 04:05 akwotom

This issue has been marked as "External" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

typescript-bot avatar May 10 '24 01:05 typescript-bot