Skip to content

How to represent rest properties in Object #201

Open

Description

@petr-motejlek

Hey.

If there is one, what is the proper way to combine rt.Dictionary and rt.Record such that the resulting static type looks like this:

type Test {
  someKey: number,
  [key: string]: string
}

Basically, what I want is a record type that has some keys that we know of, and their values have specific types, but it also can have other keys, but their will be of another specific type :). (Sort of "every person has the same fields and we use custom types to type their values, but each person can have some extra fields, and the values

rt.Dictionary().And(rt.Record()) and rt.Record().And(rt.Dictionary()) seem to kinda override each other, ie. the dictionary seems to want to always take precedence and enforce the values in the record, even though the record specified completely different types for the values.

I wonder if this would require some kind of Symbol for rt.Record, such that I don't even have to use the dictionary. Something like rt.Record({rt.Default}: rt.String) (rt.Default would be a symbol).

Activity

  1. yuhr commented on Mar 26, 2021

    @yuhr
    Member

    Currently we have no option for this AFAIK (I can't believe this hasn't been an issue until 2021 🤣) We need something like this, absolutely! Thanks for pointing out.

    One thing to say, the type you provided is invalid at least in TS 4.2.3:

    type Test = {
      someKey: number,
      [key: string]: string
    }
    // Property 'someKey' of type 'number' is not assignable to string index type 'string'.

    But you can:

    type Test = {
      someKey: number,
      [key: string]: unknown
    }

    So I'd mark this as an upstream matter, at least partially: microsoft/TypeScript#7765, microsoft/TypeScript#17867. I'm not sure it would eventually be supported in TS or not, but anyway, we'd support this preserving forward compatibility for the upstream solution...

    I think your proposal is the correct way to achieve this. Let's call the symbol Rest, then the API would look like this:

    Record({ [Rest]: Unknown })
    // Without any function call, the key type will be String by default 
    
    Record({ [Rest(Symbol)]: Unknown })
     // With a function call, you can specify some type available as key type in the current TS
  2. petr-motejlek commented on Mar 26, 2021

    @petr-motejlek
    Author

    Ah yes. Actually, I have written in as type, but the place where I got it from had it as an interface :D, eg.

    interface Test {
      someKey: number,
      [key: string]: string
    }
    

    And that does not raise any errors. I know there's subtle differences between types and interfaces, and seems like this is one of them. Not sure why, though :)

  3. yuhr commented on Mar 26, 2021

    @yuhr
    Member

    No, as noticed in microsoft/TypeScript#7765 (comment), that won't work too. Also see microsoft/TypeScript#7765 (comment), you can define a type without error by intersecting, but it can't be used properly. This seems to be a very restriction of the current type system. Hope it will be resolved...

  4. changed the title [-]How to combine Dictionary and Record[/-] [+]How to represent rest properties in `Record`[/+] on Mar 29, 2021
  5. petr-motejlek commented on Mar 29, 2021

    @petr-motejlek
    Author

    Hopefully that will change soon. For now, I'll have to type the dictionary value with all the types that are contained within the record (as well as the type(s) that I expect the actual rest property to have), as I should do even when using the plain types/interfaces from TS, as you pointed out :)

  6. ivands commented on Dec 4, 2023

    @ivands

    Also need this feature

  7. changed the title [-]How to represent rest properties in `Record`[/-] [+]How to represent rest properties in `Object`[/+] on Jan 20, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ Proxy