I have a library, which has map
function with these definitions:
function map<T, U>(f: (x: T) => U, a: Array<T>): Array<U>
function map<T, U>(f: (x: T) => U, a: Functor<T>): Functor<U>
Also, I have another library with Maybe
type, which extends Functor
, so it's possible to use it with map
function above.
Can I extend "library1" map
function with definition below, so when I use map
function from "library1" it has 3 overloads(2 above and 1 below)?
function map<T, U>(f: (x: T) => U, a: Maybe<T>): Maybe<U>
In 1st library, module structure is the following(library typings are generating automatically with declaration: true
setting):
// lib/types/index.d.ts
export interface Functor<T> {
map: <T1>(fn: (a: T) => T1) => Functor<T1>;
}
export interface MapFunction {
/**
* Array
*/
<T, T1>(f: (x: T) => T1, a: Array<T>): Array<T1>;
/**
* Functor
*/
<T, T1>(f: (x: T) => T1, a: Functor<T>): Functor<T1>;
}
export declare const map: MapFunction;
// lib/index.d.ts
export * from "./lib/types"
Next, in package.json
, I have "typings": "lib/index.d.ts"
One way to do this is to rename the types you want to extend when you import them:
import { Functor, MapFunction as _MapFunction, map as _map } from './lib'
// guessing at './lib', you might have a different project structure
interface Maybe<T> extends Functor<T> {
map<T1>(f: (x: T) => T1): Maybe<T1>;
}
interface MapFunction extends _MapFunction {
<T, T1>(f: (x: T) => T1, a: Maybe<T>): Maybe<T1>;
}
const map: MapFunction = _map; // assuming map is implemented generically
Hope that helps.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.