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

why is type T = (x: T) => void valid whereas T = T isnt? #12525

Closed
zpdDG4gta8XKpMCd opened this issue Nov 27, 2016 · 4 comments
Closed

why is type T = (x: T) => void valid whereas T = T isnt? #12525

zpdDG4gta8XKpMCd opened this issue Nov 27, 2016 · 4 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@zpdDG4gta8XKpMCd
Copy link

nightly build Nov 27, 2016:

type U = U; // problem: Type alias 'U' circularly references itself.
type T = (value: T) => void; // ok, but why?
@HerringtonDarkholme
Copy link
Contributor

https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#310-type-aliases

Note that object type literals, function type literals, and constructor type literals do not depend on types referenced within them and are therefore permitted to circularly reference themselves through type aliases.

@DanielRosenwasser
Copy link
Member

DanielRosenwasser commented Nov 28, 2016

The restriction that a type alias can't be referenced by itself at the top level has been the behavior since we implemented type aliases; however, you might recall that a while back, we started allowing type aliases to be referenced from within an object type.

If we desugar T's declaration to use object type, we can that under the current rules, it's allowed.

type T = {
    (value: T) => void
};

Overall this is actually pretty reasonable - even Haskell can't express something like this:

type Wat = Wat
<interactive>:1:1: error:
    Cycle in type synonym declarations:
      <interactive>:1:1-14: type Wat = Wat

However, it has no problem provided that the type can be expanded, or "unrolled" one level at a time. In other words, as long as you have some sort of box-y thing containing the type:

newtype Box a = Box a

the type system will be okay.

@DanielRosenwasser DanielRosenwasser added the Question An issue which isn't directly actionable in code label Nov 28, 2016
@zpdDG4gta8XKpMCd
Copy link
Author

is there any chance for recursive types in unions?

type Expression = Binary <Expression> | Unary <Expression>

@mhegazy
Copy link
Contributor

mhegazy commented Nov 28, 2016

is there any chance for recursive types in unions?

tracked by #6230

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
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

4 participants