Skip to content

Suggestion for improvement of runtime type checking mechanism in language's design #46893

Closed

Description

@galzi

I read about how detecting types and hierarchy of types of variables at runtime. It is from the first place wasn't built in a great way. Using typeof only works on primitives and return a string, and instanceof works but only for classes.

It would be great if it worked similarly to the way it works in Python. I don't know how exacly it links an object to its type, but Python's object also works as dictionaries (aka associative array, the same way JS objects work), and there's a link to the type object, so if you call a method, it will do that via the type object, and call it as typeobject(obj, parameter1, ...).

So, basically, in runtime you have the object itself, the type object which is also bound to a variable, and the type "type", which itself is also a type object.

In the case of TypeScript, you have various types of types and they can also be derived into subtypes with generics.

So, ideally, if you called typeof you'll get back the type object of the object, which is represented by its type (like: class, interface, or can just be represnted by its keys and their type, union, function, etc...), its name and possibly its variation in case its generic, e.g. interface Box<string> .

That way, comparison of types via typeof should be generalized to any possible type, and not just for primitives. In case of Python, you would check the type like that, for instance: type(1) == int, which means call the "type" type constructor with the object "1", and check equality, by its == operator with the int symbols content, which means check if they're refering to the same object (so is could work here as well, in Python).

It would be great that instanceof could also work on anything which has inheritance tree, and overall on any type, even such which can't be extended.

If I'm correct, there's actually a reference in objects to their classes, at object.prototype, so it can easily be extented to TS's own types of types, like interfaces which doesn't exist in JS.

I know it has to supports JS's quirks (and that's annoying you can't check types appropriately at runtime and you're limited to primitives), so some possible solutions could be:

  • additional compiler's option
  • using the "type" type c'tor, like Python does
  • supporting operator overriding for that use (typeof actually will return a type, by the suggestion, so type should handle the equality check, pobably by some function added to the types for that string comparison with a type), so it will return true for the basic primitives' types.

Also, having a "type" type can allow easily to reflect at runtime, like extracting the name of a type, for instance.

What do you think about this suggestion? Do you find it necessary? Do you think it will improve the experience in TypeScript? Obviously it will have some overhead, since it requires some tweaks as to how TS works at runtime, and has more stuff in the background, but not by much since JS already works much in this manner.

Activity

  1. MartinJohns commented on Nov 22, 2021

    @MartinJohns
    Contributor

    This is explicitly out of scope for TypeScript. See the TypeScript Design Goals.

  2. RyanCavanaugh commented on Nov 22, 2021

    @RyanCavanaugh
    Member

    Please follow the template. In particular, what you're describing here violates two entrance criteria listed in the feature request template:

    * [ ] This wouldn't change the runtime behavior of existing JavaScript code
    * [ ] This could be implemented without emitting different JS based on the types of the expressions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Out of ScopeThis idea sits outside of the TypeScript language design constraintsSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions