Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

empty class acts as "any" type: by design or bug? #5451

Closed
ziriax opened this issue Oct 29, 2015 · 5 comments
Closed

empty class acts as "any" type: by design or bug? #5451

ziriax opened this issue Oct 29, 2015 · 5 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@ziriax
Copy link

ziriax commented Oct 29, 2015

The following typechecks fine:

class Empty {
}

// From a strongly-typed point of view, this feels strange
var e1: Empty = "Strange";
var e2: Empty = 42;

This also compiles

class Foo {
    name: string;

    constructor(name: string) {
        this.name = name;
    }

    hello() {
        return "456";
    }
}

class Bar {
    name: string;

    hello() {
        return "123";
    }
}

var foo = new Foo("Jo");

// From a strongly-typed point of view, this feels strange
var bar: Bar = foo;

Is this by design?

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Oct 29, 2015
@RyanCavanaugh
Copy link
Member

@binarez
Copy link

binarez commented Nov 16, 2015

I'm using typescript in an educational situation and would find useful to have a stricter mode that doesn't allow this special case. Is there any way to disable this? I found a work-around : adding a dummy private member makes not empty (obviously).

class Empty
{
    private dummy : any;
}

let e : Empty = 2;

Type 'number' is not assignable to type 'Empty'. Property 'dummy' is missing in type 'Number'.

I use '_' instead of 'dummy' in my lib (empty File class, it's really just a handle for the user, no attributes or methods) but you get the idea.

@RyanCavanaugh
Copy link
Member

There isn't a way to disable this behavior using a flag or other setting. Adding a member to each class is the best solution.

@binarez
Copy link

binarez commented Nov 17, 2015

Is adding one an option or is it too tied to the type system? I haven't read the type system docs you provided above, yet.

@RyanCavanaugh
Copy link
Member

This isn't something we'd add an option for. It's a structural type system, so empty types are equivalent by definition.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants