Skip to content

Instantly share code, notes, and snippets.

@pjlsergeant
Created June 21, 2021 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjlsergeant/db677a9d6f0af90ba330be7c402d598f to your computer and use it in GitHub Desktop.
Save pjlsergeant/db677a9d6f0af90ba330be7c402d598f to your computer and use it in GitHub Desktop.
A comprehensive 5-line introduction to generics
const wrapP = <T>( x: T ) : Promise<T> => Promise.resolve(x);
const wrapL = <T>( x: T ) : T[] => [x];
const foo = wrapP("string"); // Promise<string>
const bar = wrapL(5); // number[]
// PS: you can also add constraints
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment