If you want to create a customized child type, you can do so by utilizing the extends
keyword and inserting it in a shared types.ts
file. This way, you can import the type from there instead of directly importing it from the library.
// src/types.ts
import { Deposit as DepositBase, Nice as NiceBase } from 'some-lib';
export interface Deposit extends DepositBase {
num3: string;
num4: string;
}
export interface Nice extends NiceBase {
deposit: Deposit;
}
Subsequently, you can implement it like this:
// src/some-lib-wrapper.ts
import * as someLib from 'some-lib';
import { Deposit, Nice } from './types'; // our enhanced types
// You have the capability to "re-type" the library functions within this wrapper module
// in order to make use of our personalized extended types
export function makeDeposit(nice: Nice): Deposit {
/* invoke someLib.makeDeposit here... */
}
// src/index.ts
import { makeDeposit } from './some-lib-wrapper'; // utilize the version with our modified types
import { Deposit, Nice } from './types';
// Make use of your custom wrapped functions & types
makeDeposit(...);