Excess Properties in TS are SUPER weird #typescript

preview_player
Показать описание
Become a TypeScript Wizard with my free beginners TypeScript Course:

Follow Matt on Twitter

Join the Discord:

Рекомендации по теме
Комментарии
Автор

I just did this! Had to add some options later for different content type. Made it so easy to refactor. I also take in a generic output <O> type in my fetch to be the expected return type - so each function that uses my fancyFetch can pass in the expected api response (return error or an ok with the passed in type)

Then after fetch, just throw on error, then my returned response has types ready to be used. I watched a lot of your youtube content before I made this recently, great teacher!

WillDelish
Автор

Clear, concise and informative! A diamond of a short in the rough. Thank you!

XIIOSRS
Автор

Hi Matt, you should make a video about

type Foo = { ... Stuff... }
const foo = { ...stuff... } as const satisfies Foo;

This one has couple of nice use cases 😊

jurijzahn
Автор

Very nice 👍 Subscribed ✅
How do you do these animations? I would be very happy to learn.

muratcan__
Автор

This is *one of* (and certainly not the only) the reasons I insist on not using implicit types anywhere. Types are there to help you find your mistakes early.

tjsr
Автор

It’s not really “excess properties”, the TypeScript compiler just automatically infers any types you don’t explicitly specify.

When it’s outside the function, without the explicit declaration, “opts” stops being a FetchOptions object and becomes a { url: string, timeOut: number } object, until you give it the explicit type, at which point it becomes a FetchOptions object again and you get errors because it doesn’t contain a property called “timeOut”.

DisturbedNeo
Автор

Does anyone know how Matt gets those smooth code transitions while editing the video? (code sliding, etc..)

willness
Автор

Is creating a FetchOptions type normal when fetching or is this just for demonstration?

BurritoBrooks
Автор

Hi, Matt! Thanks for all the stuff you are doing to help people understand ts better. Tell us please how can we get those nice looking error outputs in vscode? Or they are just edited in the video?

НаильШайхинуров-пл
Автор

but if typescript doesnot care about timeOut within object, stil the "timeout" key will be missing, why doesnot it complain then

yashrajjaiswal
Автор

ℹ️ For those who fail to replicate: don’t forget to add a “?” in “timeout?: number;”

qitpess