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

Consuming generics in Javascript #49890

Closed
5 tasks done
wenq1 opened this issue Jul 13, 2022 · 1 comment
Closed
5 tasks done

Consuming generics in Javascript #49890

wenq1 opened this issue Jul 13, 2022 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@wenq1
Copy link

wenq1 commented Jul 13, 2022

Suggestion

Currently javascript cannot consume typescript generic functions, resulting in warnings ts(2339) in ReactDom's useParams ().

Prototype in .d.ts file:

export function useParams<Params extends { [K in keyof Params]?: string } = {}>(): Params;

Consumption in javascript:

   const   { mode = 'default' } = useParams ();
                  ^ Property 'mode' does not exist on type '{}'. ts(2339)

🔍 Search Terms

javascript consume generics

✅ 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.

⭐ Suggestion

It is currently possible to do something like this in javascript:

/**
 * @typedef {{mode: string, mode2: string}} TestParams
 */

   const   { mode = 'default' }  = /** @type { () => TestParams } */ (useParams) ();

Is there a way to do something like this instead?

   const   { mode = 'default' }  = /** @useTemplate <TestParams> */ (useParams) ();

📃 Motivating Example

💻 Use Cases

See above

Also found #27387

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jul 14, 2022
@RyanCavanaugh
Copy link
Member

Same as #27387

@wenq1 wenq1 closed this as completed Jul 15, 2022
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

2 participants