Skip to content

Instantly share code, notes, and snippets.

@JesusTheHun
Last active September 4, 2024 14:53
Show Gist options
  • Save JesusTheHun/935569ed4962cca3ef7b23d035c96e2f to your computer and use it in GitHub Desktop.
Save JesusTheHun/935569ed4962cca3ef7b23d035c96e2f to your computer and use it in GitHub Desktop.
Partial Type Argument Inference
declare function get<Doc, Key extends keyof Doc = infer>();
declare let unknownObj: unknown;
declare let obj: { title: string; description: string };
// CASE 0
const case0 = get(obj, 'description');
// Unchanged: string
// CASE 1
const case1 = get<{ title: string; description: string }>(unknownObj, 'description');
// Current: TS Error
// Proposal: string
// CASE 2
const case2 = get<{ title: string; description: string }, 'title'>(unknownObj, 'description');
// Current: ❌ missing type argument
// Proposal: ❌ 'description' cannot be assigned to 'title'
// CASE 3
// Currently, you have to write
declare function floatingGenericTypeArgument<A, B = any>(b: B): [A, B]
// With this proposal, you could write
declare function floatingGenericTypeArgument<A, B = infer>(b: B): [A, B]
const case3 = floatingGenericTypeArgument<'a'>(null);
// Current: ['a', any]
// Proposal: ['a', null]
@JesusTheHun
Copy link
Author

Submit your case with the following :

  • Function signature
  • Function call
  • Expected return type

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment