Skip to content

Omit index signature with generic type constraints? #10941

Closed
@JabX

Description

@JabX

TypeScript Version: nightly 2.1-20160914

I have a base class that takes a generic parameter with a constraint that is an index signature. Something like

interface Constraint {
    [key: string]: number
}

class Base<T extends Constraint> {
    entity: T
}

This class is meant to be inherited from and provided with another type that satisfies the index signature but doesn't carry it itself. Something like

interface MyType {
    prop: number;
    label: number;
    code: number
}

class MyClass extends Base<MyType> {
    // Error: Index signature is missing from MyType
}

I don't want to put the index signature on MyType because destructuring entity won't output an error when the property doesn't exist:

const {id} = this.entity // oops, no error

which is a critical feature for me.

I don't want to remove the index signature from the constraint because the base class completely revolves around it and I would lose a lot of type safety.

Is there something I missed? Should we have some new language feature to solve this issue?

(Now that I think of it I suppose it's a generic assignability issue between types, not restricted of type constraints, and it's not solved by #7029 since it's only about actual litterals, not types)

Metadata

Metadata

Assignees

No one assigned

    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