question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Record with a required key

See original GitHub issue

Is it possible to create a record (e.g. Record<string, Struct>) which has one required key? I’m trying to get the equivalent of something like the this in TypeScript:

interface Type {
  foo: Struct;
  [key: string]: Struct;
}

I tried using an union with object and record, but that results in something like

type Type = { foo: Struct; } | Record<string, Struct>;

Which results in “type ‘string’ can’t be used to index type”-like errors elsewhere. I currently solved it by using refinement and specifying a custom type as generic parameter, but I’d like to know if there’s an easier solution to accomplish this.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
ianstormtaylorcommented, Nov 24, 2020

My guess is that you’re looking for something like:

const MyArray = array(...)

const MyObject = intersection([
  type({ requiredKey: MyArray }),
  record(string(), MyArray)
])
0reactions
Mrtenzcommented, Nov 26, 2020

That seems to work great, thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Record with required keys and string index [duplicate]
keys () and trying to map to Report object, which I feel is just bad implementation. Why don't you try this for iterating...
Read more >
Python - Maximum record value key in dictionary
In this, we iterate through each key for keys and assign to max, the maximum of required key in nested record.
Read more >
Documentation - Utility Types - TypeScript
Record <Keys, Type>. Released: 2.1. Constructs an object type whose property keys are Keys and whose property values are Type . This utility...
Read more >
How does the TypeScript Record type work? - Tim Mousk
The TypeScript Record<Keys, Type> utility type is used to construct a new type whose properties are Keys and values are Type.
Read more >
Write Key Record (QC3WRTKR, Qc3WriteKeyRecord) API - IBM
Required Parameter Group. Qualified keystore file name: INPUT; CHAR(20). The keystore file where the key will be stored. The first 10 characters contain...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found