Skip to content
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

Add a keyword for skipping optional type parameters in generics (to fallback to the default definition) #60550

Closed
6 tasks done
parzhitsky opened this issue Nov 21, 2024 · 1 comment

Comments

@parzhitsky
Copy link

πŸ” Search Terms

unset type parameter default

βœ… Viability Checklist

⭐ Suggestion

The suggestion is to add a keyword that says "I don't need to specify this optional type parameter, I am only passing by to a later one, please resolve to whatever the fallbace default type

πŸ“ƒ Motivating Example

Considering the case:

import express from 'express'

interface CreateUserParams {
    name: string
    age: number
}

export type CreateUserRequest = express.Request<
    Record<string, string>, // ❌ unrelated, but there's no way if skipping this
    any,                    // ❌ could've specified it, but decided to not bother
    CreateUserParams        // βœ… actually what is needed and relevant
>

Try it.

All of the type parameters of express.Request are optional. In the case when I only need to specify the third parameter, there is no way of skipping the first two. Instead, I have to go to the definition, see what the fallback type is, and provide the fallback type here explicitly; this gets words if the fallback type isn't exported.

Sometimes this can be mitigated by a better design, sometimes not.

The suggestion is to add a keyword (e.g., CSS's unset or SQL's default) that would do the lookup itself:

export type CreateUserRequest = express.Request<unset, unset, CreateUserParams>

This might be a breaking change for codebases where unset is an existing identifier. If that's a concern, consider ? as the keyword:

export type CreateUserRequest = express.Request<?, ?, CreateUserParams>

πŸ’» Use Cases

  1. What do you want to use this for? – Skipping optional parameters in generics defined by upstream libraries.
  2. What shortcomings exist with current approaches? – The need to lookup the definition.
  3. What workarounds are you using in the meantime? – I look the definition up.
@MartinJohns
Copy link
Contributor

Duplicate of #26242.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants