资料内容:
Freedom Through Restriction
TypeScript allows us to specify what types of values may be provided for parameters
and variables. Some developers find having to explicitly write out in your code how
particular areas are supposed to work to be restrictive at first.
But! I would argue that being “restricted” in this way is actually a good thing! By
restricting our code to only being able to be used in the ways you specify, TypeScript
can give you confidence that changes in one area of code won’t break other areas of
code that use it.
If, say, you change the number of required parameters for a function, TypeScript will
let you know if you forget to update a place that calls the function.
In the following example, sayMyName was changed from taking in two parameters
to taking one parameter, but the call to it with two strings wasn’t updated and so is
triggering a TypeScript complaint: