An array consisting of functions that specifically retrieve keys from an object

I have a user object with properties like id and name.

interface User {
  id: string;
  name: string;
}

There is also an UpdateUserBody type that allows updating only the 'name' property of the user in the backend.

type UpdateUserBody = Pick<User, 'name'>;

My goal is to create an array of functions that specifically return a single property of the UpdateUserBody.

Initially:

[
  () => user.name
] as Array<() => keyof UpdateUserBody>

The issue arises when I try to add () => user.id to the array, as it doesn't throw any error:

[
  () => user.name,
  () => user.id,
] as Array<() => keyof UpdateUserBody>

Explanation behind my approach

I'm utilizing Vuejs watchers to keep track of these properties for saving a draft of a new user being created by the logged-in user. This is done through:

const unwatchUserUpdateRequest = watch(
  [
    () => user.name,
  ] as Array<() => keyof UpdateUserBody>,
  updateUserApiRequest,
)

I've attempted various methods but haven't been able to resolve this issue yet. Any suggestion on how to constrain the array to only accept functions returning properties of UpdateUserBody?

Check out the Playground here

Answer №1

To ensure that the user.id is not accessible, you can make modifications to the user type. One approach is to create functions in a separate scope and pass the user as a parameter:

interface User {
  id: string;
  name: string;
}

type UpdateUserBody = Pick<User, 'name'>;

function getUserUpdateWatchers(user: UpdateUserBody): (() => string)[] {
  return [
    () => user.name,
    () => user.id, // Property 'id' does not exist on type 'UpdateUserBody'.
  ]
}

const unwatchUserUpdateRequest = watch(
  getUserUpdateWatchers(someUser),
  updateUserApiRequest,
)

If your goal is simply to extract keys without any additional functionality, you could use a list of keys along with a generic function that maps the keys to () => user[key] functions:

const unwatchUserUpdateRequest = watch(
  // Type '"id"' is not assignable to type '"name"'.
  toPropertyExtractors<UpdateUserBody>(['name', 'id'], user),
  updateUserApiRequest,
)

function toPropertyExtractors<T>(keys: (keyof T)[], object: T): (() => T[keyof T])[] {
  return keys.map(key => () => object[key])
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Having trouble deactivating date selections in Vue.js Material Datepicker

Can someone assist with the following HTML code: <datepicker :date="child" :option="option" v-model="child.dob" @change="updatechildDOB(child)" :disabled="disabled"></datepicker> Here are the available options: option: { type: 'd ...

Determine the type of a function to assign to the parent object's property

Consider the following scenario: class SomeClass { public someProperty; public someMethodA(): void { this.someProperty = this.someMethodB() } public someMethodB() { ...some code... } } I need the type of somePropert ...

It appears there was a mistake with [object Object]

Hey there, I'm currently working with Angular 2 and trying to log a simple JSON object in the console. However, I keep encountering this issue https://i.stack.imgur.com/A5NWi.png UPDATE... Below is my error log for reference https://i.stack.imgur.c ...

Utilize Element UI autocomplete to transfer data into a VueJS-powered table's input fields

I am currently working on designing an invoice form using the Element UI framework. I have successfully integrated an autocomplete feature that retrieves data from the loadAll array. Upon clicking the add button and selecting an item_name from the autocomp ...

Is it possible for me to activate a function on a different component using my header?

Hi there, I'm curious about how Vue routing and the tree structure work together. In my setup, I have a parent router that contains both my router-view and header at the same level. I want to be able to trigger some functions from my header component ...

What are your thoughts on the combination of Vue.js and Codenvy

I have been attempting to set up a basic Vue.js workspace, but everything I try seems to be unsuccessful. Whether I build it from scratch or use a Git repository, nothing seems to work at all. Is it even possible to get it to work? To Reproduce: Create ...

Jest's --findRelatedTests fails to identify associated test cases

Whenever I execute the command jest --bail --findRelatedTests src/components/BannerSet/BannerSet.tsx , an unexpected message is displayed: I couldn't locate any tests and hence exiting with code 1 If you want to exit with code 0 even when there are n ...

TypeScript anonymous class-type Higher Order Component (HOC)

Struggling with creating a generic Higher Order Component (HOC) due to type issues. Let's start with a simple example: class Abc extends React.Component<{}> { hello() { } render() { return <View /> } } Referenc ...

Error message: Cypress Vue component test fails due to the inability to import the Ref type (export named 'Ref' is missing)

Recently, I created a Cypress component test for my Vue component by mounting it and verifying its existence. The component utilizes Vue's Ref type and is structured as a TypeScript component. However, during the test execution, Cypress encountered a ...

Utilizing Highcharts with React and Typescript for Event Typing

Implementing Highcharts Events in TypeScript I have integrated custom events into my Highcharts using the React wrapper. One example is to toggle the legend when entering and exiting full screen mode. const options: Highcharts.Options = { chart: { e ...

Converting an existing array into a TypeScript string literal type: A step-by-step guide

Converting TypeScript Arrays to String Literal Types delves into the creation of a string literal type from an array. The question raised is whether it's feasible to derive a string literal from an existing array. Using the same example: const furnit ...

An issue occurred when attempting to use the Mailgun REST API from an Angular 6 application. The error message states that the 'from' parameter is missing

I am encountering an error that states the 'from' parameter is missing in the body. Can you help me troubleshoot why this issue is happening? export class EmailService { constructor(private http: HttpClient) {} sendMailgunMessage() { const opti ...

Error message: Injector Error: R3InjectorError(Standalone[_AppComponent])[_WebService -> _WebService -> _WebService] occurred

Being a student, I must apologize in advance for any mistakes in terminology or gaps in my understanding. I am currently developing an Angular front-end to communicate with my backend API. However, I keep encountering the following error in the web page c ...

Tips on how to efficiently update or insert an object within an array in Vue JS 2

My current item list is displayed below. I am looking to add new items to the list, but if the ID matches an existing entry, I want to update the value of that object. For example: segmentValues: [ { id:1, value:'Foo' }, ...

Adding a custom property to a React component

Currently, I am facing an issue while attempting to modify an MUI component. Everything works smoothly until I execute the build command, at which point it fails. I have experimented with a few variations of this, but essentially I am looking to introduce ...

Ways to determine if a date matches today's date within a component template

I am currently displaying a list of news articles on the webpage and I want to show the word "Today" if the news article's date is equal to today's date. Otherwise, I want to display the full date on the page. Is there a way to compare the news.D ...

A peculiar quirk with Nuxt.js radio buttons: they appear clickable but are somehow not disabled

I’m having an issue with a radio button that won’t check. It seems to be working fine on other pages, but for some reason it just won't click here. <div class="form-group"> <label class="control-label&q ...

This error occurs when trying to assign a value to a property of a variable that is currently undefined

Having some issues with assigning the latitude and longitude values to a variable in my code. I am able to retrieve them correctly, but when trying to use them in another method (onUpload()), I am facing some errors. export class latlonComponent implement ...

What are the steps to lift non-React statics using TypeScript and styled-components?

In my code, I have defined three static properties (Header, Body, and Footer) for a Dialog component. However, when I wrap the Dialog component in styled-components, TypeScript throws an error. The error message states: Property 'Header' does no ...

Vue: Implement out-in transition where the incoming element appears before the outgoing element has completely disappeared

Check out my code on Codepen: here In this scenario, I have set up two div elements: Block 1 and Block 2. The functionality I am looking for is when a button is clicked, Block 1 smoothly translates to the left until it goes out of view. Once that happens ...