Type nested objects. #119

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

Originally created by @tmerse on GitHub (Mar 21, 2024).

Trying to type nested objects seems unnecessarily verbose.
According to the documentation, typing objects is possible via classes or modules.

In order to type an object with nested properties, at least two class declarations are needed.

foo: Foo = new {
  bar = 23
  nested {
    baz = "nested_object_properrty"
  }
}

class Foo {
  bar: Number
  nested: Nested
}

class Nested {
  baz: String
}

Compared to e.g. TypeScript interfaces or types and other constructs to define types inline this seems a little verbose.

Sth. like anonymous classes (https://github.com/apple/pkl/issues/331#issuecomment-2005233277) seem more suitable for some of such cases considering one of pkl's stated advantages (https://pkl-lang.org/main/current/introduction/comparison.html#general-purpose-langs)

Pkl code often resembles the configuration it generates.

class Foo {
  bar: Number
  nested: class {
    baz: String
  }
}

Is something like this planned or are there other means to achieve this?

Thank you

Originally created by @tmerse on GitHub (Mar 21, 2024). Trying to type nested objects seems unnecessarily verbose. According to the documentation, typing objects is possible via classes or modules. In order to type an object with nested properties, at least two class declarations are needed. ``` foo: Foo = new { bar = 23 nested { baz = "nested_object_properrty" } } class Foo { bar: Number nested: Nested } class Nested { baz: String } ``` Compared to e.g. TypeScript interfaces or types and other constructs to define types inline this seems a little verbose. Sth. like anonymous classes (https://github.com/apple/pkl/issues/331#issuecomment-2005233277) seem more suitable for some of such cases considering one of pkl's stated advantages (https://pkl-lang.org/main/current/introduction/comparison.html#general-purpose-langs) > Pkl code often resembles the configuration it generates. ``` class Foo { bar: Number nested: class { baz: String } } ``` Is something like this planned or are there other means to achieve this? Thank you
Author
Owner

@bioball commented on GitHub (Mar 22, 2024):

We don't have any particular plans, but it's admittedly a pain point.

One possible solution is anonymous classes, like I mentioned in that comment. We're also thinking about whether we'd want to introduce structural types, which might provide another solution for pain points like this.

@bioball commented on GitHub (Mar 22, 2024): We don't have any particular plans, but it's admittedly a pain point. One possible solution is anonymous classes, like I mentioned in that comment. We're also thinking about whether we'd want to introduce structural types, which might provide another solution for pain points like this.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/pkl#119