If the parameter type is "never", the ReturnType will be "any"

short tale

const xyz = (a:never):number=>1
    
type b = ReturnType< typeof xyz> //any

const xyz2 = (a:number, b:never):number=>1

type b2 = ReturnType< typeof xyz2> //any

playground

If any parameter type is never, the ReturnType becomes any. Is there a solution to this?

lengthy tale (for those interested)

I've developed a utility type that enforces no undefined values for partial object members, empty objects, or excess object members, and it's working flawlessly

type _NoExcessEmptyUndefined<M, T> = M extends T
    ? keyof M extends keyof T
        ? keyof M extends never
            ? never
            : undefined extends M[keyof M]
            ? never
            : M
        : never
    : never

type something = {a?:number,b:boolean,c:string}

const xyz = <M extends something>(data:_NoExcessEmptyUndefined<M,something>):number=>1

const emptyObject = {}
const undefinedPartial = {a:undefined,b:true,c:"1"}
const excessMember = {b:true,c:"1",d:1}

xyz(emptyObject) // error if empty object
xyz(undefinedPartial) // error if partial member is undefined
xyz(excessMember) // error if excessMember

const success1 = {b:true,c:"1"}
const success2 = {a:1, b:true,c:"1"}

xyz(success1) // no error
xyz(success2) // no error

type haha = ReturnType<typeof xyz> // haha is any <-- problem
type hihi = Parameters<typeof xyz>[0] // hihi is never

playground

Answer №1

I am unsure why this would return any in your situation, but creating a customized version of the ReturnType function that functions correctly is possible:

type MyReturnType<F extends Function> = F extends (...args: infer A) => infer R ? R : never;

Link to Interactive Code Playground

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

Experiencing Compatibility Issues: Next.js 14.0.3 with next-auth and NestJS Backend Runs Smoothly in Development Environment, but Encounters

Currently, I am developing a Next.js 14.0.3 application that utilizes next-auth for authentication. The application interacts with an external NestJS backend for authorization and JWT validation. While everything functions correctly in the development envi ...

Error Alert: Redux unable to retrieve data from Django API due to CORS issue

Currently, I am working on a project with a frontend application running on http://localhost:3000 and a backend API on http://localhost:8000. However, I am facing a CORS issue when trying to make requests from the frontend to the backend API. The error me ...

Having trouble making ngMouseEnter (and other similar commands) function correctly

I'm currently working with Bootstrap 4 on Angular 6, and I have a delete button that I want to change its icon when the cursor hovers over it. I've tried using various ng functions like ngMouseOver and ngMouseUp, but none seem to be effective in ...

In TypeScript, a subclass can be assigned as a value of a superclass-type, but it cannot be directly used as a parameter in a function that specifically

class Parent { str = 'a'; } class ParentExtended extends Parent { num = 1; } class MyClass { static property?: Parent static method (p: Parent): void {} static func?: (pParam: Parent) => void } const pe: ParentExtended = { str: &ap ...

Having trouble with installing npm package from gitlab registry

I recently uploaded my npm package to the GitLab package registry. While the upload seemed successful, I am facing an issue trying to install the package in another project. When I run npm install, I encounter the following error: PS E:\faq\medu ...

How can we set up the Typescript Compiler to recognize typings and modules effectively?

I have been working on a TypeScript project with the following structure: <work folder>/Scripts/ (project root) +-- App +--- subfolder1 +--- subfolder2 +-- typings After openi ...

Despite setting the esModuleInterop flag, I am still encountering an error with default imports

My React project with TypeScript is causing some issues. In the main tsx file, the import React from 'react' line works fine. However, in my test file, I keep getting the TS1259 error. I suspect there might be a problem with my TS/Jest/Babel conf ...

Using Promise.all like Promise.allSettled

I am looking to streamline the handling of Promise.allSettled in a more generic way. Currently when using allSettled, it returns a list of both fulfilled and rejected results. I find this cumbersome and do not want to handle it everywhere in my code. My g ...

Can you explain the purpose of the `never[]` parameter type as defined in the TypeScript Handbook?

Exploring the topic of Inferring Within Conditional Types, an illustrative example is provided: type GetReturnType<Type> = Type extends (...args: never[]) => infer Return ? Return : never; type Num = GetReturnType<() => number>; type ...

Tips on excluding node_modules from typescript in Next.js 13

I am constructing a website in the next 13 versions with TypeScript, using the src folder and app directory. When I execute `npm run dev`, everything functions correctly. However, upon building, I encounter this error... ./node_modules/next-auth/src/core/l ...

Using Ionic 2 pipe to filter a dynamically changing list based on the name property

Q) What could be causing the error in my code's syntax? I am attempting to filter a list of items by their name property. However, the following code is resulting in the error: Cannot read property 'toLowerCase' of undefined Please note: ...

Angular2 and Typescript paired with Visual Studio 2013

Currently, I am utilizing the angular2 QUICKSTART and encountering an issue where Visual Studio fails to recognize Angular2 with typescript import Modules. However, everything else seems to be functioning correctly: https://i.stack.imgur.com/0s46Y.jpg Th ...

What is the process for importing a map from an external JSON file?

I have a JSON file with the following configuration data: { "config1": { //this is like a map "a": [ "string1", "string2"], "b": [ "string1", "string2"] } } Previously, before transitioning to TypeScript, the code below worked: import ...

Issue with MUI DialogTitle Ignoring SCSS Customization

I've been attempting to integrate a Google font into the text found in a Material-UI DialogTitle component. Here's my setup in App.module.scss: @import url('https://fonts.googleapis.com/css2?family=EB+Garamond:ital,wght@1,600&display=sw ...

Struggling to Enforce Restricted Imports in TypeScript Project Even After Setting baseUrl and resolve Configuration

I am facing challenges enforcing restricted imports in my TypeScript project using ESLint. The configuration seems to be causing issues for me. I have configured the baseUrl in my tsconfig.json file as "src" and attempted to use modules in my ESLint setup ...

I cannot access the 'isLoading' state in React Query because it is undefined

Since updating to the latest version of react query, I've been encountering an issue where the 'isLoading' state is returning undefined when using useMutation. Here's the code snippet in question: const useAddUserNote = (owner: string) ...

Encountering an issue saving files in Angular 2 when the npm server is active

Encountering an issue when trying to save .ts or .html files while npm is running 1: DoJoin(aka DoJoin) [native array.js:~129] [pc=0000035BB365DBB2] (this=0000005A3F604381 <undefined>,w=000003CB8840CFF1 <JS Array[104]>,x=104,N=0000005A3F6 ...

While implementing a delete feature in a CRUD Angular application, why am I encountering a compilation error stating that the property _id does not exist in the array?

Encountering an error while working on a MEAN app with Angular, as the compiler does not seem to accept direct manipulation of the array using the this keyword. Here is an example where the code triggers a compilation error: deleteTodo(todo) { this.t ...

Is tsconfig.json necessary for JS libraries without TypeScript to include a .d.ts file when shipping?

I am currently in the process of creating a .d.ts file for an established JavaScript library that does not utilize the TypeScript compiler or include any TypeScript code. Should I include a tsconfig.json file in the library to ensure proper interpretation ...

Comparing dates in Angular 6 can be done by using a simple

Just starting with angular 6, I have a task of comparing two date inputs and finding the greatest one. input 1 : 2018-12-29T00:00:00 input 2 : Mon Dec 31 2018 00:00:00 GMT+0530 (India Standard Time) The input 1 is retrieved from MSSQL database and the in ...