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 ...
随机推荐
- 阿里云ECS hadoop+spark+zookeeper+hive code-server 集群搭建
懒得重新排版然后发到博客了.用在线文档看吧 https://www.kdocs.cn/l/srV6o8rABW9V 用线上IDE(code-server)写scala的时候,出现BUG可以参考下面两篇 ...
- 数据库内核——基于HLC的分布式事务实现深度剖析
DTCC 2019 | 深度解码阿里数据库实现 数据库内核--基于HLC的分布式事务实现深度剖析-阿里云开发者社区 https://developer.aliyun.com/article/70355 ...
- CPU处理器架构和工作原理浅析
CPU处理器架构和工作原理浅析 http://c.biancheng.net/view/3456.html 汇编语言是学习计算机如何工作的很好的工具,它需要我们具备计算机硬件的工作知识. 基本微机设计 ...
- poj 2752Seek the Name, Seek the Fame
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536KB 64bit IO Format: %I64d &am ...
- Spring Boot 整合 Freemarker
Spring Boot 整合 Freemarker 1.Freemarker 简介 2.Spring Boot 整合 Freemarker 2.1 创建工程 2.2 创建类 2.3 其他配置 原文地址 ...
- JVM 线上故障排查
JVM 线上故障排查 Linux 1.1 CPU 1.2 内存 1.3 存储 1.4 网络 一.CPU 飚高 寻找原因 二.内存问题排查 三.一般排查问题的方法 四.应用场景举例 4.1 怎么查看某个 ...
- (22)zip命令:压缩文件或目录&&unzip命令:解压zip文件
1.zip 命令基本格式如下: [root@localhost ~]#zip [选项] 压缩包名 源文件或源目录列表 注意,zip 压缩命令需要手工指定压缩之后的压缩包名,注意写清楚扩展名,以便解压缩 ...
- 我们到底为什么要用 IoC 和 AOP
作为一名 Java 开发,对 Spring 框架是再熟悉不过的了.Spring 支持的控制反转(Inversion of Control,缩写为IoC)和面向切面编程(Aspect-oriented ...
- 一次小模块的使用过程-LC12S无线模块介绍
前言: 最近帮人做了个小设备,使用了无线模块.触摸芯片,主要功能就是把触摸按键的信号无线传到控制继电器输出,MCU是STM8系列的芯片,其中使用过程中调试无线模块LC21S觉得挺好用的,就写了这篇文章 ...
- UVA 10294 项链与手镯 (置换)
Burnside引理:对于一个置换\(f\), 若一个着色方案\(s\)经过置换后不变,称\(s\)为\(f\)的不动点.将\(f\)的不动点数目记为\(C(f)\), 则可以证明等价类数目为\(C( ...