Skip to content

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

Closed

Description

nightly build Nov 27, 2016:

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

Activity

  1. HerringtonDarkholme commented on Nov 28, 2016

    @HerringtonDarkholme
    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.

  2. DanielRosenwasser commented on Nov 28, 2016

    @DanielRosenwasser
    Member

    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.

  3. zpdDG4gta8XKpMCd commented on Nov 28, 2016

    @zpdDG4gta8XKpMCd
    Author

    is there any chance for recursive types in unions?

    type Expression = Binary <Expression> | Unary <Expression>
    
  4. mhegazy commented on Nov 28, 2016

    @mhegazy
    Contributor

    is there any chance for recursive types in unions?

    tracked by #6230

  5. locked and limited conversation to collaborators on Jun 19, 2018
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

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions