Skip to content

Allowing extracting ReturnType of wrapped function with generics #54797

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
5 tasks done
dogukanakkaya opened this issue Jun 27, 2023 · 3 comments
Closed
5 tasks done

Allowing extracting ReturnType of wrapped function with generics #54797

dogukanakkaya opened this issue Jun 27, 2023 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@dogukanakkaya
Copy link

dogukanakkaya commented Jun 27, 2023

Suggestion

Allowing extracting ReturnType of wrapped function with generics.

export function query() {
  const getData = <T extends string>(select: T = 'id,name' as T) => {
    return db.from('table').select(select)
  }
  
  return { getData }
}

type QueryWrapper = ReturnType<typeof query>

type DataWithIdAndName = ReturnType<QueryWrapper['getData']<'id,name'>>
// type DataWithIdAndName = Awaited<ReturnType<QueryWrapper['getData']<'id,name'>>>
// type DataWithIdAndName = NonNullable<Awaited<ReturnType<QueryWrapper['getData']<'id,name'>>>>

🔍 Search Terms

  • Typescript ReturnType with args
  • Typescript wrapped function ReturnType with args
  • Typescript ReturnType with generic definition

✅ Viability Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, new syntax sugar for JS, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.

💻 Use Cases

export function query() {
  const getData = <T extends string>(select: T = 'id,name' as T) => {
    return db.from('table').select(select)
  }
  
   return { getData }
}

const data = await query().getData('id,name') // this is already infered since the underlying library already returns correct types depending on select value (in my case supabase)

But when it's needed to extract that return type and use it in somewhere else. For example in a React Component that is passed as prop. I need to extract the type of getData with the generic select value that is provided.

Possible workarounds at the moment

Set generic into wrapper

export function query<T extends string>() {
  const getData = (select: T = 'id,name' as T) => {
    return db.from('table').select(select)
  }
  
   return { getData }
}
// this results with other type errors.

Or write another function and get it's type. Which is just duplication.

const fn = await query().getData('id,name')
type FnReturn = Awaited<ReturnType<typeof fn>>
@fatcerberus
Copy link

Your query function as written doesn’t actually return anything, but I’m going to assume that was a transcription error.

This looks like a duplicate of #40179.

@dogukanakkaya
Copy link
Author

dogukanakkaya commented Jun 27, 2023

Just updated the return.

It looks like a duplicate but not exactly. Wrapped functions are bit more different. If it wasn't wrapped, i could just pass generic to that function easily with. ReturnType<typeof getData<'id,name'>>

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jun 28, 2023
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants