Repository navigation
empty class acts as "any" type: by design or bug? #5451
Description
Activity
- addedQuestionAn issue which isn't directly actionable in codeAn issue which isn't directly actionable in code
on Oct 29, 2015 RyanCavanaugh commented
on Oct 29, 2015 MemberMore actionsYes. Note that the empty type does not behave as
any- it is a valid target for all assignments, but is only a valid source for assignments to other empty types:var e: Empty, f: Foo; f = e; // Error
TypeScript uses a structural type system, see also:
http://stackoverflow.com/questions/22385775/typescript-type-check-failed-to-catch-wrong-assignment/22386927#22386927
http://stackoverflow.com/questions/32106253/why-does-typescript-implicitly-convert-between-unrelated-classes/32106416#32106416
http://stackoverflow.com/questions/28080106/typescript-type-confusion-variable-doesnt-mind-type/28080519#28080519I'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 commented
on Nov 16, 2015 MemberMore actionsThere isn't a way to disable this behavior using a flag or other setting. Adding a member to each class is the best solution.
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 commented
on Nov 17, 2015 MemberMore actionsThis isn't something we'd add an option for. It's a structural type system, so empty types are equivalent by definition.
- locked and limited conversation to collaborators
on Jun 19, 2018
The following typechecks fine:
This also compiles
Is this by design?