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

SetParameterType for functions #675

Closed
innermatrix opened this issue Sep 1, 2023 · 3 comments · Fixed by #722
Closed

SetParameterType for functions #675

innermatrix opened this issue Sep 1, 2023 · 3 comments · Fixed by #722
Labels
help wanted Extra attention is needed type addition

Comments

@innermatrix
Copy link

innermatrix commented Sep 1, 2023

Just like we have SetReturnType, I'd like to see SetParameterType for individual parameters (this is useful for mocking / testing).

For example

SetParameterType<typeof fn, 0, string | null>

would be the same function type as fn except with first param type changed to string | null

@sindresorhus
Copy link
Owner

Sounds useful 👍

@sindresorhus sindresorhus added help wanted Extra attention is needed type addition labels Sep 1, 2023
@Emiyaaaaa
Copy link
Collaborator

I'd like to try to implement this type, but I'm wondering if there are any other use-cases except mock/testing? Or is it ok to add this type just for mock/test? @sindresorhus @innermatrix

@sindresorhus
Copy link
Owner

According to ChatGPT:


Certainly! Here are some use-cases for the SetParameterType TypeScript type:

  1. Mocking & Unit Testing:
    When writing unit tests, you often need to mock functions and their parameters. With SetParameterType, you can easily change the parameter type of a function to something more mock-friendly.

    function fetchData(endpoint: string, headers: object): Promise<any> { /* ... */ }
    
    type MockedFetchData = SetParameterType<typeof fetchData, 0, string | null>;

    Now, while writing tests, you can easily handle cases where the endpoint might be null.

  2. Wrapper Functions:
    If you're writing a wrapper function around an existing API and you want to make it more lenient or add additional types, SetParameterType can be useful.

    function calculateArea(width: number, height: number): number { /* ... */ }
    
    type CustomCalculateArea = SetParameterType<typeof calculateArea, 0, number | string>;

    This allows you to create a custom version of calculateArea where the width can also be a string, and internally you might parse it into a number.

  3. Creating Overloads:
    When creating overloads for existing functions to handle additional types, SetParameterType can be a quick way to derive the new function types.

    function printMessage(message: string, priority: number): void { /* ... */ }
    
    type UrgentMessagePrinter = SetParameterType<typeof printMessage, 1, "urgent" | "low-priority">;
  4. Adapting to Changing APIs:
    If you're working with third-party libraries or APIs that might change over time, SetParameterType can help in creating types that are adaptable to changes.

    function connectToDB(connectionString: string, options: DBOptions): DBConnection { /* ... */ }
    
    type FutureConnectToDB = SetParameterType<typeof connectToDB, 0, URL>;

    This can represent a future version of connectToDB where the connection string has been replaced by a URL object.

  5. Type Enhancements for Legacy Code:
    If you have legacy code where some function parameters were not strictly typed, and you want to enhance them without changing the original function, SetParameterType can be handy.

    function legacyLogger(data: any): void { /* ... */ }
    
    type EnhancedLogger = SetParameterType<typeof legacyLogger, 0, string | Error>;
  6. Middleware & Interceptors:
    In scenarios where you are writing middleware or interceptors, you might want to modify the type of certain parameters to inject additional data or change their types.

    function apiHandler(req: Request, res: Response): void { /* ... */ }
    
    type CustomApiHandler = SetParameterType<typeof apiHandler, 0, CustomRequest>;

    Here, CustomRequest might be an extended version of the original Request type with additional properties.

These are just a few scenarios where SetParameterType can be particularly useful. It provides a flexible way to adapt and modify function types based on varying requirements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed type addition
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants