Skip to content

Union of generic indexed types works incorrectly #26608

Closed
@IIIristraM

Description

@IIIristraM

I have two generic indexed types

type ExtendedService<T> = {
    [K in keyof T]: T[K] & {
        __$daemonMode?: string;
        __$action?: string;
    };
};

type Service<T> = {
    [K in keyof T]: T[K] & {id?: string};
};

And use them like this

export const createService = <T>(
    ServiceCtr: ExtendedService<T> & Service<T>
) => {
    ...
    Object.keys(ServiceCtr).forEach(key => {
        const method = (ServiceCtr)[key as keyof T];
        const {__$daemonMode, __$action, id} = method;
        ...
    }
}

VS Code output for method type is

const method: ({ [K in keyof T]: T[K] & {
    __$daemonMode?: string;
    __$action?: string;
}; } & Service<T>)[keyof T]

Expected behavior:

properties __$daemonMode, __$action, id exists on method variable;
const {__$daemonMode, __$action, id} = method; has no errors;

Actual behavior:

I got an error

[ts] Type '(ExtendedService<T> & Service<T>)[keyof T]' has no property '__$daemonMode' and no string index signature.

__$action and id produces the same error;

P.S.

Expression

const x: (ExtendedService<{z: {}}> & Service<{z: {}}>)['z'];
const {__$daemonMode, __$action, id} = x;

works as expected

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScript

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions