TypeScript 4.9 Upgrade: The Power of Type Guards
HTML-код
- Опубликовано: 23 авг 2024
- If you want to properly narrow object types or errors then this video is for you. During this tutorial you will learn how to write a type guard and how TypeScript 4.9 will improve the type checking.
My FREE programming apps:
- apps.apple.com...
- play.google.co...
My FREE TypeScript Course:
- typescript.tv/
Timeline:
00:18 - Catch clause variable type annotation (TS1196)
00:40 - Object is of type unknown (TS2571)
01:03 - Object is possibly null (TS2531)
01:37 - Property does not exist on type (TS2339)
02:56 - Changes in TypeScript 4.9
03:28 - How to update TypeScript
04:42 - VS Code's TypeScript Version
05:08 - Local exectuables from Node Modules
05:26 - TypeScript Errors Overview
Resources:
➜ github.com/mic...
➜ docs.npmjs.com...
➜ typescript.tv/...
Follow TypeScript TV:
🌐 typescript.tv
🐦 / typescripttv
📺 / typescripttv
Hashtags:
#TypeScript #typescripttutorial #nodejs #nodejstutorial
You can use "error instanceof Error" if you want to match objects derived from the built-in Error object. ☝
Your way of explanation is awesome!! Keep that up big guy :)
What about just `const in_ = (o: O, k: K): o is O & Record => typeof o === 'object' && o !== null && k in o;`? It's shorter, version-independent, and was used for years.
It's always good to know a fallback in case you are running on older TypeScript versions. 👍
When I use "Object.setPrototypeOf" I get message,
" error TS2550: Property 'setPrototypeOf' does not exist on type 'ObjectConstructor'. Do you need to change your target library? Try changing the 'lib' compiler option to 'es2015' or later."
But in tsConfig file I have already set,
"lib": [
"es5",
"es2015",
"ESNext",
"DOM"
],
Why I am getting such error?
Hi Damodara, try to specify just one version of ECMAScript. If you have "ESNext" defined, you can remove "es5" and "es2015". Hope it helps! Best, Benny
@@TypeScriptTVIt still doesn't work for me. Actually this error I don't appear while writing the script. This error only appears when I try to compile with tsc command.
@@devT44 what's your setting for "target"? Can you reproduce the problem here and share it with me? www.typescriptlang.org/play/
@@TypeScriptTV
I used "tsc --init" command.
In tsconfig file,
"target": "ES2016" ,
"lib": [
"ESNext",
"DOM"
],
Other options are not changed.
I can't produce such issue on typescript playground.
@@devT44 do you have "@types/..." dependencies installed? My advice is to click on "TS Config" on TS Playground and verify that your local config is the same (as the problem does not seem to show up on TS Playground).