当前位置:网站首页>Typescript null and undefined
Typescript null and undefined
2022-06-11 07:59:00 【YY little monster】
Details visible
1.null and undefined
TypeScript There are two special types , null and undefined, They have value respectively null and undefined.
By default, we can set null and undefined Assign to any type
By default null and undefined You can also assign values to each other
Be careful : In enterprise development , If you don't want to put null and undefined Assign to other types
Or don't want null and undefined Assign values to each other , Then we can open strictNullChecks
If we turn it on strictNullChecks, I also want to put null and undefined Assign to other types
Then we must use union types when declaring
let value:(number | null | undefined);
value = null;
value = undefined;
For optional attributes and optional parameters , If it's on strictNullChecks, By default, the data type is a union type
It's the current type + undefined type
//name The type is (string|null|undefined)
class Person {
name?:string
}
//age The type is (number|null|undefined)
function say(age?:number) {
}
2. Remove null and undefined Detection of
Use ! To remove null and undefined
function getLength(value:(string | null | undefined)) {
value = 'abc';
return ()=>{
// return value.length; // Report errors
// return (value || '').length;
// return (value as string).length;
// We can use ! To remove null and undefined
// ! The meaning of this variable is that this variable must not be null and undefined
return value!.length;
}
}
let fn = getLength('lihui.66666');
let res = fn();
console.log(res);
边栏推荐
- [atcoder1998] stamp Rally
- Image data enhancement (translation, rotation, brightness transformation, flipping, adding Gaussian noise, scaling, cropping)
- Space geometry
- Remote office experience | community essay solicitation
- Rabin Miller prime test
- Socket [5] - struct linker usage
- 134. 加油站
- C- print 99 multiplication table
- Figure seamless database integration tushare interface
- Shell编程笔记
猜你喜欢

安卓初中级开发基础知识整理(面试自用)

Lesson 1 about Xiaobai's C language

A detailed explanation of one of the causes of dead loop caused by array out of bounds in C language

Space geometry

C language lesson 2

Using Tkinter to realize guessing numbers game

Printing diamond of beginner C

签到体系设计:签到功能该怎么画

Method summary of creating deep learning model with keras/tensorflow 2.9

Record a murder case caused by ignoring the @suppresslint ("newapi") prompt
随机推荐
Long dialogue in June 2017
空间几何
Zero foundation self-study SQL course | union joint query
放大镜子效果图
Login and parameterization of interface test
SOCKET【5】- struct linger 用法
C language - Growth Diary -03- function definition and function prototype declaration
Pycrypto packaging error
Sort - select sort
C. Managing history (greedy / hashing / thinking / good questions)
【HDU6357】Hills And Valleys(DP)
[atcoder1983] BBQ hard (combination number + clever model transformation)
如何开始参与开源社区
2021-10-24
[noip2016 d1t3] changing classrooms (expectation dp+floyd) (trap of extreme thinking!)
[atcoder1984] wide swap
Summary of force deduction questions
How to start participating in the open source community
How to find the complementary sequence of digraph
Session and session management technology