How to reference a type declaration file for Javascript in another folder?

Viewed 421

I am trying to write typescript declaration files for my Javascript files, but wishing to store them in a separate folder:

src/
  types/
    foo.d.ts
  foo.js
// foo.d.ts
export function foo(bar): number
// foo.js
export const foo = bar => bar.length
// The type suggestion when calling foo from another file 
foo() //(alias) const foo: (bar: any) => any

Now when I import and call foo in other files, the type suggestion doesn't work. When I do something like this before, I had to put the .d.ts file in the same directory as the JS file. I tried adding /// <reference/> but it didn't help or I have done it incorrectly. I am not trying to export the declaration files, and it is purely for helping me during development. How can I do this?

1 Answers

Are you saying you wish to first write declaration files as sort of a contract, and then write in the matching implementation later? As sort of a "declaration-driven" development?

I'm not sure you can do that. Declaration files are for Typescript consumers of non-Typescript code. You're already in Typescript. I think what you're saying is akin to the coexistence of two definitions of the same type.

(As you may know, creating declarations files is an automatic process when you set the declaration property in your tsconfig.json or with the command line flag such as tsc --declaration.)

Related