Borek Borek - 1 month ago 8
TypeScript Question

TypeScript: function alias in .d.ts

U function in a Node module I'm trying to describe in

.d.ts
has two aliases,
config()
and
load()
(see the source). The function can be described like this in
dotenv/index.d.ts
:

export function config(options?: dotenvOptions): Object;


How do I also export it under the other alias,
load()
?

Answer

You can declare a type:

declare type ConstructorMethod = (options?: dotenvOptions) => Object;

(name can be better)

Then export the two methods as the same type:

export const config: ConstructorMethod;
export const load: ConstructorMethod;

P.S. Looks like the return type is actually object | boolean. Union types might be annoying, but I thought I might point you in this direction in case you become annoyed in future: https://www.typescriptlang.org/docs/handbook/advanced-types.html (the section on type guards and differentiating types). Just something cool I only recently came across.