Skip to content

Define types when assigning constructor functions using const #18967

Closed
@aaronlahey

Description

@aaronlahey

Is there any way currently (or any desire to implement a way) to automatically create a type when the return value of a function includes { new } and is assigned to a const?

Code

Desired Syntax

class Struct<T extends object> {
    public static define<T extends object>(): { new(t: T): Struct<T> } {
        return Struct;
    }
}

const Point = Struct.define<{
    x: number,
    y: number
}>();

// [ts] Cannot find name 'Point'.
function identity(p: Point): Point {
    return p;
}

Current Syntax

class Struct<T extends object> {
    public static define<T extends object>(): { new(t: T): Struct<T> } {
        return Struct;
    }
}

type PointData = { x: number, y: number };
type Point = Struct<PointData>;
const Point = Struct.define<PointData>();

function identity(p: Point): Point {
    return p;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions