Skip to content

Intersection type with partial not working #16251

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

Closed
jvanbruegge opened this issue Jun 4, 2017 · 4 comments
Closed

Intersection type with partial not working #16251

jvanbruegge opened this issue Jun 4, 2017 · 4 comments
Labels
Bug A bug in TypeScript
Milestone

Comments

@jvanbruegge
Copy link

jvanbruegge commented Jun 4, 2017

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>'.
@ikatyang
Copy link
Contributor

ikatyang commented Jun 4, 2017

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

@KiaraGrouwstra
Copy link
Contributor

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

@mhegazy mhegazy added the Needs Investigation This issue needs a team member to investigate its status. label Aug 29, 2017
@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript and removed Needs Investigation This issue needs a team member to investigate its status. labels Aug 23, 2019
@RyanCavanaugh RyanCavanaugh added this to the Backlog milestone Aug 23, 2019
@ashmind
Copy link

ashmind commented Jan 15, 2023

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

@jakebailey
Copy link
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.

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

No branches or pull requests

7 participants