Skip to content
0:00 / 0:00

#typescript#returntypes#typeinference

Should you use return types?

Matt Pocock

429K subscribers • 8 videos on Hallway

YouTube
  • Recommended by 3 people: 3 in engineering communities.
  • Presented by Matt Pocock
  • An opinion. It argues one side of a question engineers disagree on.

Intermediate. Assumes working knowledge.

44K views3 years ago3 recommendations

Programming languagesTools and practiceTypeScriptRefactoring and legacy code

Matt Pocock argues that developers should generally avoid explicit return types in TypeScript, except in specific scenarios like library code.

Your default attitude towards return types should be 'don't use them', unless one of three things are true: - You're writing a function with multiple branches - You're building a library - You have a specific TS perf concern and you're banging your head against the wall Become a TypeScript Wizard with Matt's free TypeScript Course: totaltypescript.com/tutorials Follow Matt on Twitter twitter.com/mattpocockuk What is a return type? Should you use them? Multiple branches in functions Library code Performance

Why it's here

3 people recommend this

What people say. Recommended for its reasonable and nuanced take on when to use explicit return types versus relying on type inference.

Moments people linked to

u/ethansidentifiablein r/typescript • Recommends

On “Are there type wizards that add types automatically?”

My personal opinion: it clutters the code, and just restates something that's already been stated in your declaration/implementation. But where it's totally defendable is function signatures, where you may want to enforce the implementation of a function to fulfill a contract. Matt Pocock has a great video on the nuances of this topic link to this video

u/davemechain r/typescript • Recommends

On “Should i allow implicit types?”

…write library code that is heavily reused. Explicit typing helps keeping the API stable. But if I'm writing e.g. an ordinary frontend component, I would never add explicit types that could also be inferred, since there is no problem in applying changes. Here is a very reasonable opinion on return types from Matt Pocock (the Typescript wizard). link to this video…

u/BritainRittenin r/typescript • Recommends

Return Type or not to Return Type: Matt Pocock gives his take as to when it's better to have explicit types on functions vs leaving it for inference
0View on Reddit