Skip to content

Can't type "bindBuild" method w/ TS 3.5.2 #28

@G-Rath

Description

@G-Rath

I've been doing the following w/ my factories:

interface Mx {
  prop1: string;
  prop2: number;
}

const MxFactory = makeFactory<Mx>({
  prop1: '',
  prop2: 1
});

const bindBuild = <T, K extends keyof T, F extends Factory<T, K>>(factory: F): F['build'] => factory.build.bind(factory);

export const buildMx = bindBuild(MxFactory);

Typically each factory lives in their own file, and then the above is done in factories/index, to make it nicer to call build. The idea is that we generally only need to call build most of the time when using factories on our tests - if we need to use the factory itself, we can import it directly.

This worked fine, until I updated to typescript 3.5.x, where the return value becomes an error:

Error:(13, 94) TS2322: Type '(...args: (RecPartial<T> & Pick<T, Exclude<keyof T, K>>)[]) => T' is not assignable to type 'F["build"]'.
  Type '(...args: (RecPartial<T> & Pick<T, Exclude<keyof T, K>>)[]) => T' is not assignable to type 'FactoryFunc<T, K>'.

This seems to be b/c FactoryFunc is a conditional, which is interesting b/c there doesn't seem to be any breaking changes relating to conditional types in 3.5.x

I know what I'm doing isn't strictly within the scope of the library itself, but was hoping you might be able to provide additional insight on what the problem could be, as you understand the types far better than me.

I can use MxFactory.build.bind(MxFactory) directly, but it'd be great if I could get this typed.

I've created an example repo here.

I have tried playing around w/ the types a fair bit, including applying the extends {} workaround for the new generic breaking change, replacing Omit w/ the new loose Omit thats now included in TS, and renaming Omit to StrictOmit in case they were clashing. The only thing that actually worked was removing the conditional :/

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions