panzerdp / dmitripavlutin.com-comments

7 stars 0 forks source link

typescript-function-type/ #162

Open utterances-bot opened 1 year ago

utterances-bot commented 1 year ago

Understanding TypeScript Function Types: A Beginner's Guide

Are you struggling to understand TypeScript function types? Look no further! This comprehensive guide covers everything you need to know.

https://dmitripavlutin.com/typescript-function-type/

stefanjudis commented 1 year ago

Thanks for the post, Dmitri. TS is still a mystery to me. 😅

Quick question, right at the beginning there's this code example:

type Sum = (a: number, b: number) => number
// Assign to variable
const sum1: Sum = function(a: number, b: number): number { return a + b } // OK

Would I define a function type and(!) add type definitions inline or should I choose one over the other? It's seems like duplicated type definitions to me. 🤔

panzerdp commented 1 year ago

Thanks for the post, Dmitri. TS is still a mystery to me. 😅

You're welcome @stefanjudis!

Would I define a function type and(!) add type definitions inline or should I choose one over the other? It's seems like duplicated type definitions to me. 🤔

Normally I'd use type inference and not duplicate the types. This example is just for demonstration.

stefanjudis commented 1 year ago

Thanks! That makes sense. 🙇‍♂️

samuelkarani commented 9 months ago

Great read. Worth mentioning object/array return types e.g. const useScreenSize: () => { width: number; height: number; screen: BreakPoint; };

amanimavu commented 5 months ago

Hello Dmitri, great read you have here. I want to know if one can define the method types as optional using "?"