Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”
The non-empty assertion operator (!)
A new postfix expression operator in context when the type checker cannot determine the type! Can be used to assert that operation objects are non-null and non-undefined
Let’s take a look at the specific usage scenarios
Omit null and undefined when assigning
const fn = (name: string | null | undefined) = > {
const objName: string = name; / / an error
}
Copy the code
Function calls ignore null and undefined
type cbGenerator = () = > string;
const fn = (cb: cbGenerator | null | undefined) = > {
cb()
}
Copy the code
The optional chain operator (? .).
The optional chain operator (? .) allows you to read the value of a property located deep in the chain of connected objects without explicitly validating that each reference in the chain is valid. ? The. Operator functions like the. Chain operator, except that it does not cause errors when references are nullish (null or undefined), and the short-circuit returns undefined. When used with a function call, returns undefined if the given function does not exist.
If the code encounters null or undefined, it can immediately stop running some expressions and return undefined
const obj = {
foo: {
bar: {
baz: 42,}}};constbaz = obj? .foo? .bar? .baz;/ / 42
constsafe = obj? .qux? .baz;// undefined
Copy the code
Null-value merge operator (??)
If the left-hand operand is null or undefined, the right-hand operand is returned; otherwise, the left-hand operand is returned.
Logic and logic or | | operators are different, or will be in the left operand is falsy value returns the right operand. That is to say, if you use the | | to set default values for some of the variables, you may encounter unexpected behavior. For example, falsy (“, NaN, or 0)
const name = null ?? 'nordon'; / / return nodedon
const age = 0 ?? 18; / / returns 0
const age2 = 0 || 18; / / return to 18
Copy the code
Optional attributes (? 🙂
An interface is an important concept in object-oriented languages. It is an abstraction of behavior that needs to be implemented by classes. Interfaces in TypeScript are a very flexible concept. In addition to abstracting some of the behavior of a class, interfaces are often used to describe the Shape of an object.
Using the interface keyword in TypeScript allows you to declare an interface:
interface Person {
name: string;
age: number;
}
let nordon: Person = {
name: "nordon"
};
Copy the code
An error will be reported if an argument is missing, in which case you need to use optional attributes
interface Person {
name: string; age? :number;
}
Copy the code
Operator (&)
Cross-typing in TypeScript combines multiple types into a single type. The & operator allows you to superimpose existing types into a single type that contains all of the required characteristics of the type
type PartialPointX = { x: number; };
type Point = PartialPointX & { y: number; };
let point: Point = {
x: 1.y: 1
}
Copy the code
Operator (|)
In the TypeScript joint type (Union Types) said value can be one of the many Types of joint Types using | separated for each type. Union types are usually used with null or undefined
const fn = (info: strong | null | undefined) = > {}
Copy the code
Numeric separator (_)
For a numeric literal, you can now group numbers by using an underscore as a separator between them
const num1 = 1 _234_567;
/ / equivalent
const num1 = 1234567;
Copy the code