Skip to content

Allow const variables with new signatures to be types #18942

@tinganho

Description

@tinganho

I want to make use of inference when defining a class. Thus, I create a function returning a class instead of defining a class the normal way:

function Model<P extends Properties>(p: P) {
    return class {
        static create() {
            create(): /*use of P somehow*/
        }
    }
}

class User extends Model({
    a: { type: Type.String }
}) {};
let user: User = new User();

Though, I think the line class User extends Model() {}; is a bit non-idiomatic-js and ugly. It would be great if I could do this instead:

function defineModel<P extends Properties>(p: P) {
    return class {
        static create() {
            create(): /*use of P somehow*/
        }
    }
}

const User = defineModel({
    a: { type: Type.String }
});
let user: User = new User(); // I can use 'User' as a type here

At the end, this is working in JS, but TS prevents me.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DeclinedThe issue was declined as something which matches the TypeScript visionSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions