What is the method for creating a type depending on the query projection?

Is there a method in TypeScript for defining a dynamic type that can be based on projection parameters?

For example, when executing a database query or GraphQL request, you might have a function like this:

interface Person {
  id: string;
  firstName: string;
  lastName: string;
}

function getPeople(projection: (keyof Person)[]): Promise<Partial<Person>[]> {...}

If I call getPeople(['id', 'firstName']), the returned objects will not include lastName since it was excluded from the projection. Is there a way to indicate this at the type level, rather than using Partial<Person> which still allows access to uninitialized properties?

Answer №1

To solve this problem, the key is to modify the getPeople function to be generic so that it can specify the types of parameters in the return type. Here's a revised version of the code that should help you achieve your goal:

interface Individual {
  id: string;
  firstName: string;
  lastName: string;
}

type Details<P, keys extends keyof P> = {
  [property in keys]: P[property];
};

declare function getPeople<keys extends keyof Individual>(
  attributes: keys[]
): Promise<Array<Details<Individual, keys>>>;

Answer №2

Simply follow these instructions

To achieve this, use the following function:
function getPeople<K extends keyof Person>(projection: K[]):
Promise<Pick<Person, K>[]>

Answer №3

interface TypeOfEntity {
  x: string;
  y: string;
  z: string;
}

const EntityItem: TypeOfEntity = { x: 'a', y: 'b', z: 'c' };

function EntityFilter<E extends keyof TypeOfEntity>(filter?: Array<E>): Pick<TypeOfEntity, E> {
  return EntityItem;
}

function execute() {
  const result = EntityFilter(['x']);
  
  result.y // will throw an error
}

Answer №4

If you want to make properties optional, simply add a question mark ? after the property name.

 interface Person {
      id: string;
      firstName?: string;
      lastName?: string;
    }

   function retrievePeople(projection: (keyof Person)[]): Promise<Partial<Person>[]> 
   {...}

By using the ? key, all properties except for id are now optional.

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

JSON and TypeScript's dynamic typing feature

Imagine you have a JSON file structured like this: { "foo": 1, "bar": true, "baz": "yes" } Is there a method to import this file and acquire static typing using TypeScript? In a regular Node.js environment, we typically use ...

Define the expected argument type of a function as an arrow function

In TypeScript, is there any way to enforce the use of arrow functions as function arguments? For instance, when using a publish-subscriber model and passing a listener function to a 'server' object, the server calls this function whenever a publi ...

Converting JSONSchema into TypeScript: Creating a structure with key-value pairs of strings

I am working with json-schema-to-typescript and looking to define an array of strings. Currently, my code looks like this: "type": "object", "description": "...", "additionalProperties": true, "items": { "type": "string" ...

React - retrieving the previous page's path after clicking the browser's "back" button

Imagine I'm on Page X(/path-x) and then navigate to page Y(/path-y). Later, when I click the "back" button in the browser. So my question is, how do I retrieve the value of /path-y in PageX.tsx? Note: I am utilizing react-router-dom ...

Ways to utilize gridster by accessing the nativeElement of ElementRef

This issue is specific to the Firefox browser and does not occur in Chrome. My goal is to access the nativeElement for the gridster element. The version of angular-gridster2 being used is v17.0.0. In the HTML code: <gridster [options]="opt ...

Guide to displaying an input box depending on the selection made in a Mat-Select component

I am working on a mat-select component that offers two options: Individual Customers and Organizational Customers. When selecting Individual Customers, the dropdown should display three options: First Name, Last Name, and Customer Id. However, when choosin ...

Retrieve information from a separate controller in Angular

Working within Angular 4, I've set up a navbar and menu component that lists various tasks pulled from a web service. Clicking on a task in the menu component opens up the edit task component, which displays a form for editing the task. The form fiel ...

Sharing information between a cordova plugin and an Angular application

I have been working on an Angular/Cordova app and I am trying to pass the online/offline status to Angular: export class AppComponent implements OnInit { isOff = false; constructor() { document.addEventListener('deviceready', onDeviceRea ...

Keep the list up-to-date by adding new items promptly

Utilizing Angular 7, I have implemented the following service (Click here for StackBlitz Example): @Injectable({ providedIn: 'root' }) export class TodoService { todos: BehaviorSubject<Todo[]> = new BehaviorSubject([ { id: 1, tit ...

Avoid using the --transpileOnly option as it may cause issues, and refrain from using the --ignoreWatch option as

Having some issues running a script on my node server using ts-node-dev with the parameters --transpileOnly and --ignoreWatch. Unfortunately, I keep encountering errors: https://i.sstatic.net/8HxlK.png Here is a snippet from my package.json: https://i.s ...

What is the best way to update this payload object?

Currently, I'm developing a route and aiming to establish a generic normalizer that can be utilized before storing user data in the database. This is the function for normalization: import { INormalizer, IPayloadIndexer } from "../../interfaces/ ...

Receive regular updates every week for an entire month using Javascript

How can I calculate the number of check-ins per week in a month using Javascript? I have been unable to find relevant code for this task. Specifically, I am interested in determining the total count of user check-ins on a weekly basis. For example, if a u ...

AWS Lambda is consistently showing a 10-second delay before the function finally ends once the code

https://i.sstatic.net/kcB03.png In a TypeScript Lambda API I developed, there is a specific point where the callback complete logs occur. Following those logs, the lambda takes approximately 10 seconds to respond as shown by the timestamps. export const h ...

Encountering an issue while trying to access an interface in Angular

I'm facing challenges while trying to access the data due to a type error. I could use some assistance. Here is a snippet of JSON data: [{ "productType": "Electronics", "modelDetails": [ { "modelId&quo ...

What is the best way to create a personalized filter function for dates in JavaScript?

I am working with a DataTable that includes a column called Timestamp: <p-dataTable sortMode="multiple" scrollable="scrollable" scrollHeight="150" [value]="currentChartData" #dt> <p-column field="timestamp" header="Timestamp" [sortable]=" ...

Creating a distinct Output type in Typescript to avoid any confusion between Output arguments and Input arguments

Inspired by C#, I am looking to define the following: type FunctionOutput<T> = T; // This is a basic implementation that needs improvement type Result = {result: number}; function myFun(a: number, b: number, c: FunctionOutput<Result>) { c.r ...

Error: Webpack 4 - Unable to locate JSON module

In my Webpack application, I have a collection of JSON files that I need to import. These files are named 0.json, 1.json, 2.json, and so on, and can be found in the directory src/res/level/. However, when I attempt to use require() to load them into my cod ...

The error message "SyntaxError: Cannot use import statement outside a module" is encountered when trying to import node-fetch in a Typescript file

I'm facing some challenges with the development of a typescript library npm package. The problem arises when I attempt to import node-fetch for fetch calls. To illustrate my issue, I have set up a sample git repository. These are the commands I used ...

Exploring Manipulation of M:N Associations in Sequelize

I have set up a sequelize schema using postgre DB export const Commune = sq.define("commune",{ codeCommune: { type: DataTypes.STRING(5), allowNull: false, primaryKey: true }, libelleCommune: { type: ...

A guide on generating instances from classes located in a directory with typescript

Imagine a scenario where there is a directory structure like the one below: Shapes/ Square.ts Circle.ts Rectangle.ts The objective here is to create a data structure, either a Map or an object, where the filename (without the extension) serves as th ...