Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for Inferring types #8

Open
denchen opened this issue Jul 14, 2024 · 1 comment
Open

Add support for Inferring types #8

denchen opened this issue Jul 14, 2024 · 1 comment

Comments

@denchen
Copy link

denchen commented Jul 14, 2024

First off, great library. My request is that mergician would infer the type of the object passed in, or at least allow a generic

From the types definition, I see this:

export function mergician(...objects: object[]): object;

So the result is always of type object. But it would be nice if we could do this:

const obj1: MyType = { ... };
const obj2: Partial<MyType> = { ... };
const newObject1 = mergician(obj1, obj2); // newObject1 is now of type MyType

const newObject2 = mergician<MyType>({}, obj1); // newObject2 is cast as MyType
@BrendonSled
Copy link

Create a mergician.d.ts in your src folder with this for the typings:

type MergicianObjectIntersection<T extends unknown[]> = T extends [infer First, ...infer Rest]
    ? First & MergicianObjectIntersection<Rest>
    : unknown;

declare module 'mergician' {
    function mergician<T extends object[]>(...objects: T): MergicianObjectIntersection<T>;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants