Because TypeScript knows what types our objects are, it also knows what shapes our objects adhere to. An object’s shape describes, among other things, what properties and methods it does or doesn’t contain.
strings, for example, are known to have a
.length property and
"OH".length; // 2 "MY".toLowerCase(); // "my"
tsc command will let you know if your code tries to access properties and methods that don’t exist:
"MY".toLowercase(); // Property 'toLowercase' does not exist on type '"MY"'. // Did you mean 'toLowerCase'?
Through this knowledge of type shapes, TypeScript helps us quickly locate bugs in our code.
Let’s try it!
More bugs! 🙀🙀🙀
We already compiled our index.ts file. Try running index.js by entering
node index.js into the bash shell. The code throws an error instead of printing the following expected output:
tsc command to see type errors in the
The TypeScript compiler is surfacing two errors. Using the error messages to guide you, fix both of the errors in the index.ts file.
Confirm that everything is now working properly. Run
tsc again to compile your TypeScript code and then run