1

I am currently working with some javascript modules with no .d.ts in typescript environment. The problem currently I am approaching is there is no suggestion in typescript file for javascript modules. For example, I have this file:

// ./test1.js

module.exports = {
    data1: "hehe",
    data2: Math.PI,
    func1: function() { return "hello, world!"; }
};

When I try to get some suggestion in javascript file, it works:

Suggestion in Javascript VSCode

But, when I try in typescript file, it not give the suggestion:

Suggestion in Typescript VSCode

The project has been setup using NextJS via pnpm dlx create-next-app@latest. I don't want to make .d.ts to satisfy the typescript intellisense, I just want the intellisense to grasp everything like what has been done with javascript suggestion, as This example above is just hypotetical situation.

In my real project, I works with some javascript modules which has no support for typescript. Even it has, I may approach the same thing when I use other javascript module or even some old one which has no active maintainer. So, I want to avoid this worst case, and instead try to config intellisense to understand js module without .d.ts file. Could this be possible? Is this a bug?

Here are my tsconfig.json and package.json

// tsconfig.json
{
  "compilerOptions": {
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true,
    "paths": {
      "@/*": ["./src/*"]
    }
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx"],
  "exclude": ["node_modules"]
}
// package.json
{
  "name": "nextjs_sample_page",
  "version": "0.1.0",
  "private": true,
  "scripts": {
    "dev": "next dev",
    "build": "next build",
    "start": "next start",
    "lint": "next lint"
  },
  "dependencies": {
    "@types/node": "20.3.1",
    "@types/react": "18.2.13",
    "@types/react-dom": "18.2.6",
    "eslint": "8.43.0",
    "eslint-config-next": "13.4.7",
    "next": "13.4.7",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "typescript": "5.1.3",
    "wrtc": "^0.4.7"
  }
}

And this is how I setup my environment: Setup with NextJS

To be short, my only question is how to make Typescript Intellisense works along with javascript library which not have type, and not have active maintainer and contributor, without creating .d.ts by myself. The library is abandoned enough which create a situation where no contributor creating the .d.ts for that library.

Farhan M Sabran
  • 57
  • 1
  • 10
  • Can you rename that file to `.ts` ? – Dimava Jun 22 '23 at 13:23
  • From `test1.js` to `test1.ts`? No. Because, I would working with some javascript modules which have no support for typescript and may have no active maintainer to make the definition. As it build on js, it may use classic `module.exports` not `export` for jsm. – Farhan M Sabran Jun 22 '23 at 13:26
  • What happens if you write `import test1 = require("./test1.js");` in your TypeScript file? – Aluan Haddad Jun 23 '23 at 02:41
  • It's unexpectedly works. But it only works with local files. When I try to import a module written in pure JS without `.d.ts`, it did not works. I try to do `import wrtc = require("wrtc");` and typescript say there is no `.d.ts` file, and no suggestion showed. – Farhan M Sabran Jun 23 '23 at 08:16
  • @FarhanMSabran you need your d.ts with `declare module "wrtc" { export const data1: 'foo' } ` then – Dimava Jun 24 '23 at 13:26
  • https://stackoverflow.com/questions/12687779/how-do-you-produce-a-d-ts-typings-definition-file-from-an-existing-javascript – Dimava Jun 24 '23 at 13:26
  • I have read that question long ago but not meet my expectation. I want typescript intellisense imitate javascript intellisense which is "automatically know the data types and properties inside it without need of definition". – Farhan M Sabran Jun 25 '23 at 05:07

0 Answers0