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.

Error when trying to use `pick` utility on a Struct constructed with `Describe` utility function

See original GitHub issue

Hi @ianstormtaylor, I followed your suggestion here https://github.com/ianstormtaylor/superstruct/issues/526#issuecomment-733209466 and everything works Ok except when I tried to use pick<Struct, ['field-1', 'field-2', ... , 'field-x']> where the Struct is defined using the Describe type utility function that helps to ensure that structs match types. For example, the following:

type User = {
  id: number
  name: string
  age: number
  address: string
}

const User: Describe<User> = object({
  id: number(),
  name: string(),
  age: number(),
  address: string()
})

const basicDetails = pick<User, ['name', 'age']>

will throw the error Argument of type 'Describe<User>' is not assignable to parameter of type 'Struct<{ [x: string]: any; }, Record<string, Struct<any, any>>>'. But if I remove : Describe<User> then it works Ok as expected.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
nwaughachukwumacommented, Dec 10, 2020

Thanks @ianstormtaylor, you’re patient and helpful.

0reactions
ianstormtaylorcommented, Dec 7, 2020

@nwaughachukwuma I don’t think you’re using the pick utility correctly, at least based on that snippet. The <...> brackets are for typings, not for runtime values. You likely want:

import {string, number, Describe, object, nullable, pick} from 'superstruct'

type Order = {
  id: string 
  pid: string | null
  userId: string
  quantity: number
}

const Order: Describe<Order> = object({
  id: string(),
  pid: nullable(string()),
  userId: string(),
  quantity: number()
})

const basicDetails = pick(Order, ['pid', 'userId'])

Notice the (...) parentheses instead. (And you need to import pick as well.)

If you were looking to use the typing-level utility that’s built in to TypeScript, you’d do:

type BasicDetails = Pick<Order, 'pid' | 'userId'>

Notice <...>, uppercase Pick, and no need to import it. And this would be creating a new type not a struct or other runtime value.

I realize the difference between compile-time typings and runtime values can often be confusing, I get tripped up on that every once in a while too. That’s the mental model complexity cost paid to have things named the same, which has other benefits.

Read more comments on GitHub >

github_iconTop Results From Across the Web

ios - Do you usually use class or struct to define a list of utility ...
I think a conversation about this has to start with an understanding of dependancy injection, what it is and what problem it solves....
Read more >
DataCamp_-_Track_-_Data_Scientist_with_R_-_Course_02_ ...
This chapter introduces you to a bunch of useful functions for data structure manipulation, regular expressions and working with times and dates. <div...
Read more >
Generate Shared Utility Code - MATLAB & Simulink - MathWorks
The code generator verifies that the current model that you are building has configuration properties that match the checksum of properties from the...
Read more >
Documentation - Utility Types - TypeScript
This type is meant to model operations like await in async functions, or the .then() method on Promise s - specifically, the way...
Read more >
Utility functions | BigQuery - Google Cloud
GENERATE_UUID · Description. Returns a random universally unique identifier (UUID) as a STRING . The returned STRING consists of 32 hexadecimal digits in...
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