[TypeScript] Use the never type to avoid code with dead ends using TypeScript
2017-02-01 01:36
295 查看
Example 1: A never stop while loop return a never type.
Example 2: Never run If block
You can use this to do exhaustive checks in union types.
For example, let's say you have a variable returned from the server that can be a string or a number. You can easily add code that handles different cases using the JavaScript typeof operator. You can add an additional else, and assign the variable to a never to ensure that all types were eliminated.
Later, if you need to add another type to the union, for example, a Boolean, you will now get nice errors at all the places where the new type was not handled, because only a never is assignable to a never. Now, if you go ahead and add another typeof to handle this new case, the error goes away.
function run(): never { while(true){ let foo = "bar"; } }
Example 2: Never run If block
const foo = 123; if(foo !== 123) { let bar: never = foo; }
You can use this to do exhaustive checks in union types.
For example, let's say you have a variable returned from the server that can be a string or a number. You can easily add code that handles different cases using the JavaScript typeof operator. You can add an additional else, and assign the variable to a never to ensure that all types were eliminated.
declare var foo: | string | number; if(typeof foo === "string") { /* todo */ } else if (typeof foo === "number"){ /* todo */ } else { const check: never = foo; }
Later, if you need to add another type to the union, for example, a Boolean, you will now get nice errors at all the places where the new type was not handled, because only a never is assignable to a never. Now, if you go ahead and add another typeof to handle this new case, the error goes away.
相关文章推荐
- 使用cocapods报错 [!] Your Podfile has had smart quotes sanitised. To avoid issues in the future, you should not use TextEdit for editing it. If you are not using TextEdit, you should turn off smart quotes
- A c++ class wrapper to simplify the use of CRITICAL_SECTION and avoid dead-lock
- jooq使用自定义策略生成代码(How to use a custom strategy with the jOOQ code-generator and Maven?)
- using sqlite in windows phone 7/successful to deal with the problem in wp to use like select chinese character
- A c++ class wrapper to simplify the use of CRITICAL_SECTION and avoid dead-lock
- Using RestTemplate, how to send the request to a proxy first so I can use my junits with JMeter?
- niagara编译错误:using type arguments to access generic types requires the use of "-source 1.5"
- How To Use the PivotTable Office Web Component with VB (转载自微软)
- Use sp_executesql to deal with the Image type Parameter in SP
- Use pthread_exit() To Terminate main() With The Other Threads Alive
- How to find the correct version of JDeveloper to use with eBusiness Suite 11i or Release 12.x
- how to use the code in ms ssrs
- Creating New DB Using DBCA on 10.2.0.3 Fails With ORA-1092 While Upgrading To The 10.2.0.3 Works Fine
- How to get the password text in a text with password property from another process using C++ - 用C++如何从不同进程获取密码框文本
- How to use "vi" edit the docs or source code files
- use code to generate Silverlight Controls at run time, and serialize the control into XAML file
- how to use a SQLite database in a standalone program with an HTML interface and VBScript as the programming language
- An error occurred during the execution of xp_cmdshell. A call to 'LogonUserW' failed with error code: '1385'
- The module ... was loaded but the call to DllRegisterServer failed with error code 0x80070005
- How to add HTML code to the clipboard by using Visual Basic Or VBA