add types to a javascript when using typescript - 07-12-21

Today I had a .js file that I needed to import into the rest of a TypeScript app but could not convert it to a .ts file. To get around this limitation I added an accompanying .d.ts file.

countryConfig.js

const countries = [
    {
        code: 'de',
        domainExtension: '.de'
    },
    ...
];

exports.countries = countries;

countryConfig.d.ts

interface Country {
    code: string;
    domainExtension: string;
}

export const countries: Array<Country>;

app.ts

import countries from './countryConfig'

...

source: https://www.typescriptlang.org/docs/handbook/declaration-files/templates/module-d-ts.html

\- [ js, typescript ]