Is that TypeScript code valid? Explain why.

Technology CommunityCategory: TypeScriptIs that TypeScript code valid? Explain why.
VietMX Staff asked 1 year ago
Problem

Consider:

class Point {
    x: number;
    y: number;
}

interface Point3d extends Point {
    z: number;
}

let point3d: Point3d = {x: 1, y: 2, z: 3};

Yes, the code is valid. A class declaration creates two things: a type representing instances of the class and a constructor function. Because classes create types, you can use them in the same places you would be able to use interfaces.