snippets/old_snippets/type-js-file.md

809 B

title date draft snippet_types
add types to a javascript when using typescript 2021-07-12T11:12:32+02:00 false
js
typescript

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