Using type alias causes error where directly using the type doesn't #96

Open
opened 2025-12-30 01:20:43 +01:00 by adam · 1 comment
Owner

Originally created by @jfharden on GitHub (Feb 29, 2024).

It is surprising (at least to me) that in the following pkl definition:

typealias Step = GetStep | PutStep

class Job {
  steps: Listing<Step>?
}

class GetStep {
  prop1: Step?
}

class PutStep {
  prop1: Step?
}

An error is thrown for prop1 on the PutStep saying:

–– Pkl Error ––
Type alias definitions must not be cyclic.

8 | prop1: Step?
           ^^^^
at foo#GetStep (file:///Users/jonathan.harden/test/pkl/foo.pkl, line 8)

1 | typealias Step = GetStep | PutStep
                     ^^^^^^^
at foo#Step (file:///Users/jonathan.harden/test/pkl/foo.pkl, line 1)

4 | steps: Listing<Step>?
                   ^^^^
at foo#Job (file:///Users/jonathan.harden/test/pkl/foo.pkl, line 4)

106 | text = renderer.renderDocument(value)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
at pkl.base#Module.output.text (https://github.com/apple/pkl/blob/0.25.2/stdlib/base.pkl#L106)

However if instead of the typealias, I use the value of the typealias directly it is fine:

class Job {
  steps: Listing<GetStep | PutStep>?
}

class GetStep {
  prop1: (GetStep | PutStep)?
}

class PutStep {
  prop1: (GetStep | PutStep)?
}

It seems I should be able to use a typealias anywhere I would use a type and get the same result.

I can't see in the documentation for type alias where this is mentioned.

Clearly the workaround is to not use a type alias and instead just redefine the complex type everywhere which is ok for now, but it's a surprising limitation.

Versions:

$ pkl --version
Pkl 0.25.2 (macOS 14.1, native)
Originally created by @jfharden on GitHub (Feb 29, 2024). It is surprising (at least to me) that in the following pkl definition: ```pkl typealias Step = GetStep | PutStep class Job { steps: Listing<Step>? } class GetStep { prop1: Step? } class PutStep { prop1: Step? } ``` An error is thrown for prop1 on the PutStep saying: ``` –– Pkl Error –– Type alias definitions must not be cyclic. 8 | prop1: Step? ^^^^ at foo#GetStep (file:///Users/jonathan.harden/test/pkl/foo.pkl, line 8) 1 | typealias Step = GetStep | PutStep ^^^^^^^ at foo#Step (file:///Users/jonathan.harden/test/pkl/foo.pkl, line 1) 4 | steps: Listing<Step>? ^^^^ at foo#Job (file:///Users/jonathan.harden/test/pkl/foo.pkl, line 4) 106 | text = renderer.renderDocument(value) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ at pkl.base#Module.output.text (https://github.com/apple/pkl/blob/0.25.2/stdlib/base.pkl#L106) ``` However if instead of the typealias, I use the value of the typealias directly it is fine: ```pkl class Job { steps: Listing<GetStep | PutStep>? } class GetStep { prop1: (GetStep | PutStep)? } class PutStep { prop1: (GetStep | PutStep)? } ``` It seems I should be able to use a typealias anywhere I would use a type and get the same result. I can't see in the documentation for type alias where this is mentioned. Clearly the workaround is to not use a type alias and instead just redefine the complex type everywhere which is ok for now, but it's a surprising limitation. Versions: ``` $ pkl --version Pkl 0.25.2 (macOS 14.1, native) ```
Author
Owner

@stackoverflow commented on GitHub (Mar 1, 2024):

We are discussing the possibility to create recursive type aliases. But, indeed, this is not currently supported.

@stackoverflow commented on GitHub (Mar 1, 2024): We are discussing the possibility to create recursive type aliases. But, indeed, this is not currently supported.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/pkl#96