Skip to content

3.5.1 (regression): "string cannot be used to index T" where T is Record<string, any> #31661

Closed
@lll000111

Description

@lll000111

This is new as of 3.5.1, there was no error in 3.4.5.

Error message:

TS2536: Type 'string' cannot be used to index type 'T'

Obviously wrong, since T is defined as Record<string, any>.

function isString (thing: unknown): thing is string {
    return typeof thing === 'string';
}

function clone<T extends Record<string, any>>(obj: T): T {
    const objectClone = {} as T;

    for (const prop of Reflect.ownKeys(obj).filter(isString)) {
        objectClone[prop] = obj[prop];
    }

    return objectClone;
}

Playground Link — It will not show an error until the Playground uses the new TS 3.5.x

Unbenannt-1

Metadata

Metadata

Assignees

Labels

Working as IntendedThe behavior described is the intended behavior; this is not a bug

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions