How to make typescript declaration file work for function types

40 Views Asked by At

This is my code:

tsconfig.js

{
  "compilerOptions": {
    "baseUrl": "./",
    "target": "es6",                          
    "module": "commonjs",                    
    "declaration": true,                   
    "strict": true,                          
    "esModuleInterop": true,                  
    "forceConsistentCasingInFileNames": true, 
    "typeRoots": [
      "node_modules/@types",
      "./@types"
    ],
    "paths": {
      "@types/*": ["@types/*"]
    },
  },
  "exclude": ["node_modules"],
  "include": ["./*", "@types"]
}

@types/index.d.ts

interface Person {
  is: boolean;
  str: string;
}

type doThing = (is: boolean) => boolean;

example.ts

const jon: Person = {
  is: 'hello world'
}

const doThing = x => `${x}`;

in example.js:

  • The interface for 'Person' is found and works
  • The type for 'doThing' is not found
1

There are 1 best solutions below

0
On

It seems the only sure way is to create a type which is named differently, but can be used onto any function.

index.d.ts

type TdoThing = (is: boolean) => boolean;

example.d.ts

const doThing: TdoThing = x => `${x}`;

const doOtherThing: TdoThing = x => `${x}`;