学习心得:一开始无法感知 TS 的好处,只是觉得编码更加麻烦,现在慢慢能体会到好处,想象你合作时使用他人创建的组件,组件加了类型定义的话,你写代码时知道哪些 props 是可选哪些是必写,写错了还会有报错提示。
H1Intro
TypeScript addresses the challenges of JavaScript, offering a strong and static type system by providing early error detection and improved tooling.
Benefits:
- Static typing
- Code completion
- Refactoring
- Shorthand notations
H1Basics
H2Static type-checking
Static types systems describe the shapes and behaviors of what our values will be when we run our programs. A type-checker like TypeScript uses that information and tells us when things might be going off the rails.
H2Non-exception Failures
A static type system has to make the call over what code should be flagged as an error in its system, even if it’s “valid” JavaScript that won’t immediately throw an error. In TypeScript, the following code produces an error about location
not being defined:
H2Types for Tooling
TypeScript can catch bugs when we make mistakes in our code, and TypeScript can also prevent us from making those mistakes in the first place by providing code completion as you type in the editor.
H2Explicit Types
Keep in mind, we don’t always have to write explicit type annotations. In many cases, TypeScript can even just infer (or “figure out”) the types for us even if we omit them.
H2Erased Types
When we compile the above function greet
with tsc
to output JavaScript:
Type annotations aren’t part of JavaScript (or ECMAScript to be pedantic), so there really aren’t any browsers or other runtimes that can just run TypeScript unmodified. That’s why TypeScript needs a compiler in the first place.
H2Downleveling
Why did rewriting the template string above happen?
Template strings are a feature from a version of ECMAScript called ECMAScript 2015 (a.k.a. ECMAScript 6, ES2015, ES6, etc. - don’t ask). TypeScript has the ability to rewrite code from newer versions of ECMAScript to older ones such as ECMAScript 3 or ECMAScript 5 (a.k.a. ES3 and ES5). This process of moving from a newer or “higher” version of ECMAScript down to an older or “lower” one is sometimes called downleveling.
By default TypeScript targets ES3, an extremely old version of ECMAScript. We could have chosen something a little bit more recent by using the target option. Running with --target es2015 changes TypeScript to target ECMAScript 2015, meaning code should be able to run wherever ECMAScript 2015 is supported.
While the default target is ES3, the great majority of current browsers support ES2015. Most developers can therefore safely specify ES2015 or above as a target, unless compatibility with certain ancient browsers is important.
H1Types
JS:
- number
- string
- boolean
- null
- undefined
- object
TS:
- any
- unknown
- never
- enum
- tuple
H2Tuples
H2Enums
H2Arrays
H2Functions
Parameter & Return Type Annotations
H3Functions Which Return Promises
If you want to annotate the return type of a function which returns a promise, you should use the Promise
type:
H2Object Types
H3Optional Properties
Add a ?
after the property name.
H2Union Types
A union type is a type formed from two or more other types, representing values that may be any one of those types.
H3Working with Union Types
The solution is to narrow the union with code.
Sometimes you’ll have a union where all the members have something in common. For example:
H2Intersection Types
H2Type Aliases
it’s common to use the same type more than once and refer to it by a single name.
H2Interfaces
If you would like a heuristic, use
interface
until you need to use features fromtype
.Heuristic: 指得是探索启发式的学习方法,在这里指你有需求了,再去使用 type,也是一种学习新知识的方式。
An interface declaration is another way to name an object type:
H3Differences Between Interfaces & Type Aliases
Extending an interface:
Interfaces:
Type Aliases:
Adding new fields to an existing interface, but a type cannot be changed after being created.
H2Type Assertions
H2Literal Types
In addition to the general types string
and number
, we can refer to specific strings and numbers in type positions.
It’s not much use to have a variable that can only have one value. But by combining literals into unions, you can express a much more useful concept.
H3Literal Inference
In the above example req.method
is inferred to be string
, not "GET"
.
You can use as const
to convert the entire object to be type literals:
const req = { url: "https://example.com", method: "GET" } as const;
H2null
and undefined
H3Non-null Assertion Operator (Postfix !
)
TypeScript also has a special syntax for removing null
and undefined
from a type without doing any explicit checking.
H1Narrowing
H2typeof
type guards
"string"
"number"
"bigint"
"boolean"
"symbol"
"undefined"
"object"
"function"
H2The in
operator narrowing
H2Discriminated unions
Updated Version:
H1More on Functions
H2Function Type Expressions
H1TSX
When you want to assign a React component type to a property of a TypeScript object, you can use React.ReactNode
as the type of property.
H1References
Videos:
Docs: