Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

248
Visualizações
How can I use .d.ts with in local javascript function

I wonder how can I use .d.ts file to local javascript files.

I know I can declare module to give a type to third-party libs.

// index.d.ts (wrote on place typeRoots)

declare module "random-joke" {
  const getRandomJoke: () => Promise<any>;
  export { getRandomJoke };
}

like this. so I can use this "random-joke" library with type.

but how about this? I am try to import { sum } function in index.ts


// module/utils.js

export function sum(a, b) {
  return a + b;
}

export function power(a, b) {
  return a ** b;
}

export function multiply(a, b) {
  return a * b;
}


import { sum } from "./modules/utils"; 

//'sum' is declared but its value is never read.ts(6133) 
//Could not find a declaration file for module './modules/utils'. 

I tried declare module like this. but it doesn't works (It would have been pretty funny if this worked)

enter image description here

Anyway, I tried everything what I know. But the only thing that worked out was set allowJS:true in tsconfig.json

I know it isn't a right answer. then how can I make typescript get types in d.ts file in fair with javascript functions?

Even a little hint would be of great help. thanks

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

ok, now I got how these things work.

just create your d.ts file with prefix of your js file.

in this case, you should make "utils.d.ts"


// module/utils.d.ts

export declare function sum(a: number, b: number): number;
export declare function power(a: number, b: number): number;
export declare function multiply(a: number, b: number): number;

then ts will get types by comparing file name.

enter image description here

as you can see, now you can use your js functions within types.

just be sure add your .d.ts file path in tsconfig.json.


{
   //...

    "typeRoots": [
      "./types",
      "./modules" // my d.ts files are in "types" and "modules"
    ] /* Specify multiple folders that act like './node_modules/@types'. */

  //...

hope this answer were help.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda