[Typescript] Typing a Function Composition with Overloads and Generics
typescript and with Function Composition generics
2023-09-14 08:59:11 时间
import { expect, it } from 'vitest';
import { Equal, Expect } from '../helpers/type-utils';
export function compose<T1, T2>(func: (t1: T1) => T2): (t1: T1) => T2;
export function compose<T1, T2, T3>(
func1: (t1: T1) => T2,
func2: (t2: T2) => T3
): (t1: T1) => T3;
export function compose<T1, T2, T3, T4>(
func1: (t1: T1) => T2,
func2: (t2: T2) => T3,
func3: (t3: T3) => T4
): (t1: T1) => T4;
export function compose<T1, T2, T3, T4, T5>(
func1: (t1: T1) => T2,
func2: (t2: T2) => T3,
func3: (t3: T3) => T4,
func4: (t3: T4) => T5
): (t1: T1) => T5;
export function compose(...funcs: Array<(input: any) => any>) {
return (input: any) => {
return funcs.reduce((acc, fn) => fn(acc), input);
};
}
const addOne = (num: number) => {
return num + 1;
};
const addTwoAndStringify = compose(addOne, addOne, addOne, String);
it('Should compose multiple functions together', () => {
const result = addTwoAndStringify(4);
expect(result).toEqual('7');
type tests = [Expect<Equal<typeof result, string>>];
});
it('Should error when the input to a function is not typed correctly', () => {
const stringifyThenAddOne = compose(
// addOne takes in a number - so it shouldn't be allowed after
// a function that returns a string!
// @ts-expect-error
String,
addOne
);
});
相关文章
- [Typescript] Creating Chainable Method Abstractions with Generics and the Builder Pattern - 05
- [Typescript] TypeScript module Augmentation
- [Typescript] Get a TypeScript package ready for release to NPM
- [TypeScript] Typescript Intersection & Union Types
- [Typescript] Prevent Type Widening of Object Literals with TypeScript's const Assertions
- [CLI] Create a Hybrid Single-Multi Command Node.js CLI with Oclif and TypeScript
- [TypeScript] Create Explicit and Readable Type Declarations with TypeScript mapped Type Modifiers
- [Typescript] Improve Readability with TypeScript Numeric Separators when working with Large Numbers
- [Webpack + React] Import CSS Modules with TypeScript and webpack
- [Vue + TS] Use Dependency Injection in Vue Using @Inject and @Provide Decorators with TypeScript
- [Typescript] Creating Chainable Method Abstractions with Generics and the Builder Pattern - 05
- [Typescript] Function scope in typescript
- [Typescript] export = and import = require()
- [Typescript] Making TypeScript Stick - 5 - Extends
- [Typescript] Dynamic types: Use TypeScript's Mapped Types and Template Literal Types Together
- [TypeScript] instanceof and Type Guards (getPrototypeOf)
- [TypeScript] String Enums and Inlining Members
- [CLI] Create a Hybrid Single-Multi Command Node.js CLI with Oclif and TypeScript
- [React] Write a stateful Component with the React useState Hook and TypeScript
- [Typescript] Improve Readability with TypeScript Numeric Separators when working with Large Numbers
- [TypeScript] Query Properties with keyof and Lookup Types in TypeScript
- [TypeScript] Using Exclude and RootDir until File Globs Lands in 2.0.
- 关于COMMIT WORK and COMMIT WORK AND WAIT在SAT中的讨论