Skip to content

Intersection type with partial not working #16251

Description

TypeScript Version: 2.2.1

Code

export type Transformer<Si> = (name: string) => (s: Array<Instances<Si>>) => Array<any>;
export type TransformerConfig<Si> = { [k in keyof Si]?: Transformer<Si> };
//export type TransformerConfig<Si> = Partial<{ [k in keyof Si]: Transformer<Si> }>; <-- same result
export type ToSinksConfig<Si> = { '*': Transformer<Si> } & TransformerConfig<Si>;
//export type ToSinksConfig<Si> = { '*': Transformer<Si> } & TransformerConfig<Si> | { '*': Transformer<Si> }; <-- works
export type Instances<Si> = { ins: Array<Si> };

class Configurator<Si> {
    public configure(config: ToSinksConfig<Si> = { '*': n => x => x }): any {
        return undefined;
    }
}

Expected behavior:
No errors

Actual behavior:

test.ts(7,22): error TS2322: Type '{ '*': (n: string) => (x: Instances<Si>[]) => Instances<Si>[]; }' is not assignable to type 'ToSinksConfig<Si>'.
  Type '{ '*': (n: string) => (x: Instances<Si>[]) => Instances<Si>[]; }' is not assignable to type 'TransformerConfig<Si>'.

Activity

  1. ikatyang commented on Jun 4, 2017

    @ikatyang
    Contributor

    For minimal reproduced code and some information:

    v2.4.0-dev.20170604

    type X1<T> = { x: number } & { [K in keyof T]?: any };
    function fn1<T>(v: X1<T> = { x: 1 }): any {};
    // unexpected error
    // Type '{ x: number; }' is not assignable to type 'X1<T>'.
    //   Type '{ x: number; }' is not assignable to type '{ [K in keyof T]?: any; }'.
    
    type X2<T> = { x: number } & { [K in keyof any]?: any };
    function fn2<T>(v: X2<T> = { x: 1 }): any {};
    // no error as expected
    
    type X3<T> = { x: number } & { [K in string]?: any };
    function fn3<T>(v: X3<T> = { x: 1 }): any {};
    // no error as expected
  2. KiaraGrouwstra commented on Jun 9, 2017

    @KiaraGrouwstra
    Contributor

    Still fails with type X1<T extends string> = { x: number } & { [K in keyof T]?: any }; (make T explicitly extend string). Huh.

  3. added
    BugA bug in TypeScript
    and removed
    Needs InvestigationThis issue needs a team member to investigate its status.
    on Aug 23, 2019
  4. ashmind commented on Jan 15, 2023

    @ashmind

    Same problem when using Partial<T> I think:

    function f<T>() {
        // Type '{ a: 1; }' is not assignable to type '{ a: 1; } & Partial<T>'.
        //  Type '{ a: 1; }' is not assignable to type 'Partial<T>'
        const x: { a: 1 } & Partial<T> = { a: 1 };
    }

    https://www.typescriptlang.org/play?#code/GYVwdgxgLglg9mABMAPAFQHwAoCUiDeAUIiYhAgM5SIAeAXAYgIYMCMiAvogGSIAKTAE6wmAG3QZEAXkYtE7DgG5CHIA

  5. jakebailey commented on Jul 8, 2023

    @jakebailey
    Member

    Just checking out old issues; this looks to have been fixed by #32071.

    For the code in the recent comment, the error looks correct to me; T could be { a: string } in which case you're assigning to never (or, just plain the fact that it could be undefined and it can't be 1 & undefined.

  6. locked as resolved and limited conversation to collaborators on Oct 21, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScript

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions