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

As a developer I'd like to use type aliases as values (esp. in JSX tags) #2111

Open
mor-n4 opened this issue Apr 13, 2021 · 1 comment
Open

Comments

@mor-n4
Copy link
Contributor

mor-n4 commented Apr 13, 2021

When implementing the first iteration of type aliases, it was a deliberate decision to disallow the use of type aliases as values, because they are types only and do not have a representation at runtime.

For example, all the following is currently disallowed by a dedicated validation:

function foo(p: constructor{?}) {}

class Cls {}

type A = Cls;

A; // error: A type alias may not be used as a value.
new A(); // error: A type alias may not be used as a value.
foo(A); // error: A type alias may not be used as a value.

However, it would be possible to allow a type alias being used as a value iff it resolves to a type that is also a value (in the above cases, for example). In that case, the transpiler would have to rewrite the reference to the type alias to a reference to the aliased / actual type (in the example above, rewrite references to A to refs to Cls).

Such a use of a type alias as value might be particularly useful in a JSX tag.

At the moment, this ticket is mainly intended to keep track of this topic. It is not yet clear if such a feature is worth the effort/complexity, so this has to be clarified before starting with this user story.

WARNING: supporting this is not trivial, because

  • the transpiler would have to insert additional imports (if the aliased type is declared in a different module than the type alias),
  • some invalid cases have to be considered (e.g. what if only the type alias is exported but not the aliased type; what if the aliased type is declared in a different project and no direct project dependency exists to that project?).
mor-n4 added a commit that referenced this issue Apr 13, 2021
…in JSX tags) -- preparations (#2112)

* improve handling of IdentifierRefs pointing to type aliases
@mor-n4
Copy link
Contributor Author

mor-n4 commented Apr 15, 2021

IMPORTANT: the above, merged PR does not resolve this ticket!
It only included some preparations, see PR description for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant