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 ...
随机推荐
- Java int和integer有什么区别 (mybatis踩坑)
不要在实体类中使用int 我们都知道Integer是int的包装类,而int是基本数据类型.所以Integer类型的变量会初始化为null,int类型则会被初始化为0 . 所以在下面的动态拼接例子中: ...
- Tensorflow-交叉熵&过拟合
交叉熵 二次代价函数 原理 缺陷 假如我们目标是收敛到0.A点为0.82离目标比较近,梯度比较大,权值调整比较大.B点为0.98离目标比较远,梯度比较小,权值调整比较小.调整方案不合理. 交叉熵代价函 ...
- charles安装使用乱码连手机等问题解决方案
捣鼓半天终于安装好了,给大家分享下我的过程 1.安装, 正常网上安装即可,我安装了个有汉化包的,,推荐链接 安装方法下载破解版,安装即可 安装包地址:https://pan.baidu.com/s/1 ...
- 从一片森林(JavaScript)到另一片森林(C++)
从JavaScript到C Plus Plus 作为一个忠诚的Web开发者,JavaScript几乎是我这一年多以来的首选,不管是开发网站后端服务,还是开发跨端应用,我都会首选一个使用JavaScri ...
- 利用powershell隐藏执行后门
运行后门时,有些后门不能中断.双击运行这种后门会产生一个黑框. 一条命令就能使其在后台执行 命令解释: start-process启动一个进程 -windowstyle窗口样式 hidden隐藏
- linux 高可用----keepalived+lvs
什么是高可用? HA(high availability)即高可用性:就是在高可用集群中发生单点故障时,能够自动转移资源并切换服务,以保证服务一直在线的机制. LVS LVS:(linux virtu ...
- Vue-Cli程序环境搭建
环境搭建 ##1.下载node.js cmd输入 node -v 查看是否能够正确打印出版本号 cmd输入 npm -v 查看是否能够正确打印出版本号 ##2.安装node.js淘宝镜像加速器 ### ...
- Git实现1个项目2个地址1次推送
Git实现1个项目2个地址1次推送 考虑到不需要pull操作,因此本方法适用于个人项目分别在两个平台或地址进行部署 给origin 增加一个可以push的地址 git remote set-url - ...
- Codeforces Round #656 (Div. 3) A. Three Pairwise Maximums
题目链接:https://codeforces.com/contest/1385/problem/A 题意 给出三个正整数 $x,y,z$,找出三个正整数 $a,b,c$ 使得 $x = max(a, ...
- poj 3278 Catch That Cow(记忆化广度优先搜索)
题意: 0到N的数轴上,每次可以选择移动到x-1,x+1,2*x,问从n移动到k的最少步数. 思路: 同时遍历三种可能并记忆化入队即可. Tips: n大于等于k时最短步数为n-k. 在移动的过程中可 ...