Log in to GraphQL Editor
Advanced typescript tutorial - return based on args
Artur

Artur Czemiel

12/12/2018

Advanced typescript tutorial - return based on args

Hello This is third article of advanced typescript tutorial series. Today I'll cover basic usage of generic functions

type Point = {
  x?: number
  y?: number
  z?: number
}
const myFunc = <T extends Point>(args: T): T => {
  return args
}

As arguments I'll provide object containing Point properties. This function will only return Partial of Point based on provided paramaters in args argument;

const result = myFunc({
  x: 1,
  y: 1,
})

And the intellisense for such function is Typescript return correct args

As you see there is no z property here. Typescript aleready knows we provided these 2 args and it should return only them!

This part is super short as I can provide infinite number of generic functions usages. Thank you for your support on `/r/typescript/ reddit ! It gives me power to write blog-post-a-day!

Check out our other blogposts

Advanced typescript tutorial - conditional types
Artur Czemiel
Artur Czemiel
Advanced typescript tutorial - conditional types
2 min read
over 5 years ago
State of GraphQL 2022 survey
Michał Tyszkiewicz
Michał Tyszkiewicz
State of GraphQL 2022 survey
6 min read
over 1 year ago
Top 5 GraphQL IDEs for 2023
Michał Tyszkiewicz
Michał Tyszkiewicz
Top 5 GraphQL IDEs for 2023
8 min read
over 1 year ago

Ready for take-off?

Elevate your work with our editor that combines world-class visual graph, documentation and API console

Get Started with GraphQL Editor