A generic TypeScript with the ability to extend optionally

How can I modify the generic ApiResBook type to handle optional props with input check using the extends keyword?

Sandbox.

I have these main types defined, which correspond to fields in a database:

// Main types as in database - should't be changed
type Book = {
  id: string
  title: string
  visible: boolean
  author: string
}

type Author = {
  id: string
  name: string
}

For the API fetch response, I need a generic type that shapes an object based on requested fields

// Inhereted from types from main
type BookFields = keyof Book
type AuthorFields = keyof Author

// type for generating expected fetch response from API
type ApiResBook<
  PickedBookFields extends BookFields,
  PickedAuthorFields extends AuthorFields | undefined = undefined,
> = {
  book: Pick<Book, PickedBookFields> & {
    author?: PickedAuthorFields extends AuthorFields ? Pick<Author, PickedAuthorFields> : undefined
  }
}

// example of fetching data from the API
async function fn() {

  const fetchAPI = <ExpectedData = any>(
    apiAction: string,
    body: any
  ): Promise<{ data: ExpectedData } | { error: true }> => {
    return new Promise((resolve) => {
      fetch(`api`, body)
        .then((raw) => raw.json())
        .then((parsed: { data: ExpectedData } | { error: true }) => resolve(parsed))
        .catch((err) => {
          console.log(err)
        })
    })
  }

  // response type is { error: true  } | {data: { book: { id: string } } }
  const response = await fetchAPI<ApiResBook<'id'>>('smth', {}) 
}

The issue lies with the generic ApiResBook type, as I am unsure how to make certain generic types optional. Test examples are provided:

//tests
type BookOnly = ApiResBook<'id'>
type BookWithAuthor = ApiResBook<'id', 'name'>

// should be ok
const bookOnly: BookOnly = { book: { id: '1' } }
const bookWithAuthor: BookWithAuthor = { book: { id: '1', author: { name: 'Max' } } }

// should result in error
type BookOnly2 = ApiResBook<'propFoesntExist'>
const bookOnlyError: BookOnly = { book: { id: '1', author: {name: 'Max'} } } 
const bookWithoutAuthorError: BookWithAuthor = {book: {id: '1'}} 

Answer №1

Managed to find a solution on my own :/ Everything seems to be working fine now.

type ApiResBook<
  PickedBookFields extends BookFields,
  PickedAuthorFields extends AuthorFields | undefined = undefined,
> = {
  book: Pick<Book, PickedBookFields> & {
    author: PickedAuthorFields extends AuthorFields ? Pick<Author, PickedAuthorFields> : undefined
  }
}

Check it out in the Sandbox.

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

Order processing in Angular 2

As a newcomer to Angular, my main focus is on understanding the order in which the files are processed within an application. To the best of my knowledge, the processing order is as follows: First, main.ts is processed where the bootstrap method associ ...

Guide to adding jquery with typings installation

Need assistance: typings install jquery --global typings ERR! message Unable to find "jquery" ("npm") in the registry. Did you want to try searching another source? Also, if you want contribute these typings, please help us: https://github.com/typings/re ...

Using TypeScript with React Context

Currently, I am diving into the world of React! Our team has made a commitment to using Typescript. I find myself in a situation where I need to create a shopping cart for a bustling shopping center. The shopping cart will be utilized in t ...

Having trouble launching an Angular 2 project

Something strange just happened to my Angular 2 project. Suddenly, I can't seem to start it anymore, and I'm stuck trying to figure out why based on the error messages... node_modules/@types/core-js/index.d.ts(21,14): error TS2300: Duplicate ide ...

Renaming personalized elements in Aurelia templates

My inquiry pertains to the process of aliasing custom elements and integrating them into aurelia's html-templates. To set the scene, I am utilizing the latest webpack typescript skeleton available at https://github.com/aurelia/skeleton-navigation and ...

A step-by-step guide to showcasing dates in HTML with Angular

I have set up two datepickers in my HTML file using bootstrap and I am attempting to display a message that shows the period between the first selected date and the second selected date. The typescript class is as follows: export class Datepicker { ...

Guide for retrieving a user object from an HTTP request

I am looking to retrieve only the user object from the request. public async getUserByHash(hash: IHash) { this.logger.log('Hash for check email accessed'); const user = await this.hashRepository.findOne({ select: ['id', ...

Node.js and Express: The error message "Cors is not a function"

Everything was running smoothly until this morning when out of nowhere, a type error popped up stating that Cors is not a function Here's my code: import * as Cors from "cors"; ... const corsOptions: Cors.CorsOptions = { allowedHeaders: ["Origi ...

Angular is giving me an undefined Array, even though I clearly defined it beforehand

I've been working on integrating the Google Books API into my project. Initially, I set up the interfaces successfully and then created a method that would return an Array with a list of Books. public getBooks(): Observable<Book[]>{ ...

Misunderstanding the concept of always being right

Here is a code snippet that raises an error in TypeScript: class Status { constructor(public content: string){} } class Visitor { private status: Status | undefined = undefined; visit(tree: Tree) { if (tree.value > 7) { this.status = new ...

Extract the array structure from the resolved promises using await with Promise.all()

When I receive the values returned by using await Promise.all() in the following manner: const [apple, banana] = await Promise.all<Object, Object>([ applePromise(), bananaPromise() ]).catch(error => next(error)); An error is triggered: T ...

Navigating in express

Here is the structure I am working with: server.ts routes/ index.ts homeRoute.ts In server.ts: let app = Express(); app.use(router); In routes/index.ts: const routes = Router(); export default function router() { routes.use('/home' ...

Tips for setting up various mock services for testing an Angular 2 component

I am working with two Mock services: @Injectable() class UserRegistrationServiceMock { registerBasicDetails(details: UserRegistrationDetails) { let response: UserRegistrationResponse = new UserRegistrationResponse(); response.success = ...

Verify that the password is entered correctly in Angular2

My Angular2 form looks like this: this.registerForm = formBuilder.group({ 'name': ['', Validators.required], 'email': ['', Validators.compose([Validators.pattern("[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+&bso ...

Why doesn't TypeScript automatically determine the prop type when Generics are used?

Below is the code snippet: interface MyInterface { a: { b: { c: "c"; }; }; } type ParentProps = keyof MyInterface type ChildProps<ParentProp extends ParentProps> = keyof MyInterface[ParentProp] type GrandChildType< ...

Fetching URL from Right Before Logging Out in Angular 2 Application

I am struggling to capture the last active URL before logging a user out of my Angular 2 app. My goal is to redirect them back to the same component or page once they log back in. Currently, I am using this.router.routerState.snapshot['url'] to r ...

What is the correct way to implement Axios interceptor in TypeScript?

I have implemented an axios interceptor: instance.interceptors.response.use(async (response) => { return response.data; }, (err) => { return Promise.reject(err); }); This interceptor retrieves the data property from the response. The re ...

webpack - compile one TypeScript file separately (2 actions)

In summary... When my Vue files are combined with background.ts, webpack processes them to create bundled vue files along with background.js I'm unable to run the background.js script I expected background.js to only contain "console.log(' ...

The module for the class could not be identified during the ng build process when using the --

Encountering an error when running: ng build --prod However, ng build works without any issues. Despite searching for solutions on Stack Overflow, none of them resolved the problem. Error: ng build --prod Cannot determine the module for class X! ...

To utilize a spread argument, it is essential for it to either be in tuple form or be supplied to a rest

I am currently learning TypeScript and working on converting my project to TypeScript. However, I encountered an error while trying to use spread arguments. I have researched this topic, but I am still unsure of the correct usage. Here is my current appro ...