TypeScript & as & Type Assertion
TypeScript & as & Type Assertion

Type Assertion (as)
That is not vanilla JavaScript, it is TypeScript. As any means consider the typed object as a plain untyped javascript object.
The as keyword is a Type Assertion in TypeScript which tells the compiler to consider the object as another type than the type the compiler infers the object to be.
"use strict";
/**
*
* @author xgqfrms
* @license MIT
* @copyright xgqfrms
* @created 2020-09-11
* @modified
*
* @description TypeScript Type Assertion
* @augments
* @example
* @link
*
*/
const log = console.log;
/* <input type="text" data-uid="html-input-type-assertion"> */
const dom = document.querySelector(`[data-uid="html-input-type-assertion"]`);
const inputDOM1: HTMLInputElement = dom as HTMLInputElement;
// const inputDOM1: HTMLInputElement = document.querySelector(`[data-uid="html-input-type-assertion"]`) as HTMLInputElement;
inputDOM1.value;
const inputDOM2: HTMLInputElement = <HTMLInputElement>dom;
// const inputDOM2: HTMLInputElement = <HTMLInputElement>document.querySelector(`[data-uid="html-input-type-assertion"]`);
inputDOM2.value;
const input = document.querySelector(`[data-uid="html-input-type-assertion"]`);
input.value;
/*
const input: Element | null
Object is possibly 'null'.ts(2531)
*/
input?.value;
/*
any
Property 'value' does not exist on type 'Element'.ts(2339)
*/
input?.nodeValue;
// (property) Node.nodeValue: string | null | undefined
as
// TypeScript
function validateToken(token: string) {
return token;
}
const token = 'kjadj' as string | undefined;
validateToken(token);
Type Assertion
https://www.typescriptlang.org/docs/handbook/basic-types.html#type-assertions
as-syntax
let someValue: unknown = "this is a string";
let strLength: number = (someValue as string).length;
“angle-bracket” syntax:
let someValue: unknown = "this is a string";
let strLength: number = (<string>someValue).length;


demos
https://www.typescriptlang.org/docs/handbook/advanced-types.html#type-guards-and-type-assertions
https://basarat.gitbook.io/typescript/type-system/type-assertion
var foo = {};
foo.bar = 123; // Error: property 'bar' does not exist on `{}`
foo.bas = 'hello'; // Error: property 'bas' does not exist on `{}`
interface Foo {
bar: number;
bas: string;
}
var foo = {} as Foo;
foo.bar = 123;
foo.bas = 'hello';
Advanced Types
Type Guards and Differentiating Types
let pet = getSmallPet();
let fishPet = pet as Fish;
let birdPet = pet as Bird;
if (fishPet.swim) {
fishPet.swim();
} else if (birdPet.fly) {
birdPet.fly();
}
refs
https://linguinecode.com/post/how-to-solve-typescript-possibly-undefined-value
https://stackoverflow.com/questions/55781559/what-does-the-as-keyword-do
xgqfrms 2012-2020
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
TypeScript & as & Type Assertion的更多相关文章
- [TypeScript] Work with DOM Elements in TypeScript using Type Assertions
The DOM can be a bit tricky when it comes to typing. You never really know exactly what you're going ...
- [golang] go的typeswitch guard(类型区别)语法和type assertion(类型断言)语法
最近在实现golang,看到个go的特性语法: typeswitch guard. typeswitch guard语法如下: package main import "fmt" ...
- 7.2 Go type assertion
7.2 Go type assertion 类型断言是使用在接口值上的操作. 语法x.(T)被称为类型断言,x代表接口的类型,T代表一个类型检查. 类型断言检查它操作对象的动态类型是否和断言类型匹配. ...
- [TypeScript] Use the TypeScript "unknown" type to avoid runtime errors
The "any" type can be very useful, especially when adding types to an existing JavaScript ...
- [TypeScript] Increase TypeScript's type safety with noImplicitAny
TypeScript tries to infer as much about your code as it can. But sometimes there really is not enoug ...
- [TypeScript] Create Explicit and Readable Type Declarations with TypeScript mapped Type Modifiers
Using the optional “+” sign together with mapped type modifiers, we can create more explicit and rea ...
- Go 的类型断言type assertion
Go语言中的类型断言,语法上是这样的: x.(T) 其中,x是interface接口的表达式,T是类型,称为被断言类型. 补充一下,接口有接口值的概念,其包括动态类型和动态值两部分. 类型断言根据T的 ...
- Go语言中cannot convert adminname (type interface {}) to type *: need type assertion的解决办法
解决的办法是把string(adminname)替换为adminname.(string).其它类型也是类似.
- go 报 need type assertion
responese_total := m["responses"].([]interface{})[0].(map[string]interface{})["hits&q ...
随机推荐
- vscode远程开发安装
https://www.cnblogs.com/xiaoqi/p/vs-code-remote.html ============================= https://blog.csdn ...
- Windows 10 安装 JDK14 Java 环境,没有 jre 包
一.下载 JDK Oracle JDK 官网下载地址:https://www.oracle.com/technetwork/java/javase/downloads/index.html 二.配置环 ...
- java.lang.IllegalStateException Unable to find a @SpringBootConfiguration错误解决方案
问题描述:java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @Co ...
- 洛谷P3413 P6754
双倍经验题 由于我先做的 P6754,所以一切思路基于 P6754 的题目 " P6754 这题就是 P3413 的究极弱化版 " --By Aliemo. P6754 Descr ...
- JQuery——相关练习
####JQuery的基本语法 <!--导入JQuery文件--> <script src="js/jquery-3.1.1.min.js"> /*带min ...
- SpringApplication.run
SpringApplication.run一共做了两件事,分别是 创建SpringApplication对象 利用创建好的SpringApplication对象,调用run方法 1.创建SpringA ...
- 一个可以在多平台运行的任天堂GameBoy模拟器
今天为大家带来一个很有趣的游戏模拟器GoBoy GoBoy GoBoy是一个可以在多平台运行的任天堂GameBoy和GameBoy Color的模拟器,目前这个模拟器可以运行大多数的GameBoy游戏 ...
- linux(3) 处理目录的常用命令
目录命令总览 ls(英文全拼:list files): 列出目录及文件名 cd(英文全拼:change directory):切换目录 pwd(英文全拼:print work directory):显 ...
- 2019 Multi-University Training Contest 2 I.I Love Palindrome String(回文自动机+字符串hash)
Problem Description You are given a string S=s1s2..s|S| containing only lowercase English letters. F ...
- POJ2774 Long Long Message 【SAM】
POJ2774 Long Long Message 找两个串的最长公共字串 对其中一个串\(s\)建\(SAM\),然后我们如何找到最长公共字串,办法就是枚举\(t\)串所有的前缀,然后找各个前缀的最 ...