Choose the object's property name using TypeScript through an interface

Consider a simplified code snippet like the following:

interface MyBase {
    name: string;
}
  
interface MyInterface<T extends MyBase> {
    base: MyBase;
    age: number;
    property: "name"   // should be: "string" but only properties from T
}    
  
const myFunc = <T extends MyBase>(item: MyInterface<T>) => {
    return item.base[item.property];
}
  
let t:MyInterface<MyBase> = {base: {name: "Chris"}, age: 30, property: "name"};
console.log(myFunc(t));  // will log "Chris"

The code above demonstrates accessing a property from a base class using the string "property" from MyInterface. This approach works because it is specifically limited to being just "name."

What I want to accomplish is restricting the property to only allow strings representing properties on the generic object T. If I were to change it to just "string," Typescript would understandably raise an error in myFunc, and I'd prefer not to resort to explicitly casting to any.

Is there a way to achieve this alternative constraint?

Thanks in advance, Christoph

Answer №1

If you want to make use of keyof, here is a slightly tweaked version of your code provided below:

interface MyBase {
  type: string;
}

interface MyGeneric<T extends MyBase> {
  base: T;
  size: number;
  attribute: keyof T; // expected to be: "string" but only properties from T
}

const customFunc = <T extends MyBase>(element: MyGeneric<T>) => {
  return element.base[element.attribute];
};

let customElement: MyGeneric<MyBase> = {
  base: { type: "John" },
  size: 25,
  attribute: "type",
};
console.log(customFunc(customElement));

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

Implement a back-to-top feature with a scroll button (Ionic 2 | Typescript)

Hello, I am currently working on incorporating a "scroll to top button" feature that includes the following requirements: Display the button once the user has scrolled down. Hide the button when the user scrolls back up. If the button is clicked ...

Checking a TypeScript project with various start points for linting

Within my extensive application that is constructed using webpack, there are numerous entry points that are generated dynamically. Each entry point requires specific target files to be created during the build process. I have already realized that when bu ...

Error encountered when asynchronously iterating over an object in TypeScript

Could someone please help me understand why I am getting an error with this code? var promise = new Promise((resolve, reject) => { resolve([1, 2, 3, 4, 5]); }); async function doSomethingAsync() { var data = await promise; data.forEach(v = ...

In Angular, set the default value of the first item in the list to be highlighted when the page loads

In my project, there are two key components: 1)contact and 2)display. The contact component is responsible for displaying a list of contacts as shown in the image below: Next to the contact component, I have placed another component called 'display& ...

Instance of "this" is undefined in Typescript class

After stumbling upon this code online, I decided to try implementing it in TypeScript. However, when running the code, I encountered an error: Uncaught TypeError: Cannot set property 'toggle' of null @Injectable() export class HomeUtils { p ...

Is there a Typescript function error that occurs when attempting to rename a key, stating it "could potentially be instantiated with a different subtype"?

I am currently working on a Mongoify type that accepts a type T, removes the id key, and replaces it with an _id key. type Mongoify<T extends {id: string}> = Omit<T, "id"> & { _id: ObjectId }; function fromMongo<T extends ...

Using RxJS to merge various HTTP requests into a unified and flattened observable array

Struggling with combining multiple http get requests simultaneously and returning them as a unified, observable array. In my current setup, the method returnNewCars() retrieves Observable<ICar[]> by executing a single http get request. However, in t ...

What is a simple way to exclude a prop from a declaration in a React/TypeScript project in order to pass it as undefined

I'm trying to accomplish this task but encountering a typescript error indicating that the label is missing. Interestingly, when I explicitly set it as undefined, the error disappears, as shown in the image. Here's how my interface is structured ...

The rapid execution of code causing an observable race condition

When exporting a CSV file in my code, I encounter a race condition while trying to modify some data before the export. The issue is that the id gets set correctly, but the number only updates after the method is called a second time. I believe the proble ...

Combining multiple arrays of objects using multiple keys with Angular 9 and Typescript

I have two JSON objects (displayed here with JSON.stringify(array of objects)) GPRows data is [ { "shopName":"Testing One", "state":"NSW", "yearMonth":"20203", "id& ...

Combining vue with deno and vscode: A guide to seamless development integration

How can I set up Visual Studio Code for a Vue project using Deno? Important note - the issues mentioned here only pertain to highlighting in VSCode, as the builds, development, and scripts are functioning correctly! Deno + Vue is an appealing choice! You ...

Using TypeScript to type styled-system props

Currently, I am utilizing styled-system and one of the main features of this library is its shorthand props that allow for simple and quick theming. Although I have streamlined my component, a significant aspect lies here: import React from 'react&a ...

What is the best way to retrieve every single element stored in an Object?

On a particular page, users can view the detailed information of their loans. I have implemented a decorator that retrieves values using the get() method. Specifically, there is a section for partial repayments which displays individual payment items, as d ...

Issues with sending emails through Nodemailer in a Next.js project using Typescript

I'm currently working on a personal project using Nodemailer along with Next.js and Typescript. This is my first time incorporating Nodemailer into my project, and I've encountered some issues while trying to make it work. I've been followin ...

Decrease the construction duration within a sprawling Angular 8 project

It takes nearly 10-15 minutes to compile the project in production mode, resulting in a dist folder size of 32MB Here are the production options I am currently using:- "production": { "optimization": true, "outputHashing": "all", ...

How to perform a fetch on a local path in Next.js?

Is there a way to use the fetch method with a relative path like this: export async function getServerSideProps() { // Fetch data from local API const res = await fetch(`/api/get_all_prices`) const data = await res.json() // Pass data to th ...

The Observable<ArrayBuffer> type does not have a property map

I encountered an issue while upgrading from Angular v4 to Angular v6. I was in the process of replacing Http and HttpModule with HttpClient and HttpClientModule. As a result, I imported HttpClient from @angular/common/http in a service to fetch results fro ...

Typescript's ability to have Enums with dynamic keys

Suppose I define: enum Sort { nameAsc = 'nameAsc', nameDesc = 'nameDesc' } Is it possible to do the following? const key = 'name' + 'Desc'; Sort[key] Appreciate any help in advance ...

Ways to retrieve a value from a span using the Document Object Model

sample.html <span id='char'>{{value}}</span> sample.ts console.log((<HTMLInputElement>document.getElementById('char'))) This code snippet will display <span id='char'>ThisIsTheValueupdated</sp ...

Testing Angular: Implementing Mock Classes and Services using the Any Data Type

When mocking services without using TestBed and instead relying on Fake Classes, is it considered a best practice to use a Mock with the : any data type? If not, errors like missing items/parameters may occur. Although spyOn can be used as an alternative, ...