Skip to content

Conversation

@HT154
Copy link
Contributor

@HT154 HT154 commented Dec 14, 2025

Resolves #1368

@HT154 HT154 force-pushed the amend-union-no-default-hint branch from 064a391 to 32478ad Compare December 14, 2025 07:59
@HT154 HT154 marked this pull request as draft December 14, 2025 08:08
@HT154 HT154 force-pushed the amend-union-no-default-hint branch from 32478ad to 43e9f2e Compare December 14, 2025 22:19
@HT154 HT154 marked this pull request as ready for review December 14, 2025 22:20
@@ -0,0 +1,17 @@
–– Pkl Error ––
Tried to read property `b` but its value is undefined.
Union type `Listing<String> | String` (from type alias `B`) has no default member.
Copy link
Member

@bioball bioball Dec 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This message doesn't explain what "Default member" is. If we want this as the error message, probably should have a suggestion of how to mark a union default member.

But, this error message feels a little strange to me for a couple reasons. This throws too:

foo: Int | Boolean | String

But having a default member doesn't help, because none of these types have default values.

Also, this error message feels oddly specific; the issue here is that the property doesn't have a default value.
There's no default value because:

  1. The class property has no declared default value
  2. The property's declared type has no default value
  3. The object instance did not assign to this property

So there's three possible resolutions:

  1. Add an explicit default in the class property (only if this is modifiable code)
  2. Change the type to have an explicit default (only if one of the member types has a default value, and this is modifiable code)
  3. Define the property in your instance

I think this current error message would probably be misleading too often.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the condition here to check that the union has no default member and updated the message a bit. How does it look now?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It still feels odd to me:

  1. Adding a default marker doesn't do anything for union types like Int | Boolean | String
  2. The code might not be modifiable, so we shouldn't have verbage that suggests updating the type as a fix.

To address #1368, it feels like the error message should be something like this?

Cannot instantiate type `Listing<String> | String` because it has no default value.

But we'd really only want to show this error message when we're trying to amend it with an object body.

@HT154 HT154 requested a review from bioball December 15, 2025 21:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Amending an undefined property with a union type with no default member should produce a more specific error

2 participants