[TypeScript] Represent Non-Primitive Types with TypeScript’s object Type
ypeScript 2.2 introduced the object
, a type that represents any non-primitive type. It can be used to more accurately type methods such as Object.create
. Don't confuse it with the Object
type or {}
, the empty object type, though!
So one thing we need to understand that the Primitive types includes: 'number, boolean, string, symbol, null, undefined'. The 'array, {}, fn()' belongs to 'object' type. The 'object' type therefore is different from 'Object' type in typescript. 'Object' type is referring empty object ( {} ).
Type 'Object' gives autocomplete. It includes all the methods which an empty object can have.
But empty object without Object type has not.
If we try to assign a value to empty object, we get error:
In order to do it, we can do:
const object: { [key: string]: any } = {};
object.foo = 'bar';
[TypeScript] Represent Non-Primitive Types with TypeScript’s object Type的更多相关文章
- [TypeScript] Model Alternatives with Discriminated Union Types in TypeScript
TypeScript’s discriminated union types (aka tagged union types) allow you to model a finite set of a ...
- [TypeScript] Dynamically Allocate Function Types with Conditional Types in TypeScript
Conditional types take generics one step further and allow you to test for a specific condition, bas ...
- [TypeScript] The Basics of Generics in TypeScript
It can be painful to write the same function repeatedly with different types. Typescript generics al ...
- electron教程(番外篇二): 使用TypeScript版本的electron, VSCode调试TypeScript, TS版本的ESLint
我的electron教程系列 electron教程(一): electron的安装和项目的创建 electron教程(番外篇一): 开发环境及插件, VSCode调试, ESLint + Google ...
- Java中的原始类型(Primitive Types)与引用类型(Reference Values)
Java虚拟机可以处理的类型有两种,一种是原始类型(Primitive Types),一种是引用类型(Reference Types). 与之对应,也存在有原始值(Primitive Values)和 ...
- Effective Java 49 Prefer primitive types to boxed primitives
No. Primitives Boxed Primitives 1 Have their own values Have identities distinct from their values 2 ...
- Implement Hash Map Using Primitive Types
A small coding test that I encountered today. Question Using only primitive types, implement a fixed ...
- C# 01 Primitive Types and Expressions
Class Data or Attributes state of the application Methods or Functions have behavior Namespace is a ...
- 玩转TypeScript(引言&文章目录) --初看TypeScript.
JavaScript过去一直被当作一种玩具语言存在,直到2005年以后,这门语言又开始活跃并可以说是火爆,而且随着浏览器版本的不断升级和完善,各种DOM之间的兼容性已经渐渐的被各种技术解决了,比如经典 ...
随机推荐
- scss常规用法
保持sass条理性和可读性的最基本的三个方法:嵌套.导入和注释. 一般情况下,你反复声明一个变量,只有最后一处声明有效且它会覆盖前边的值. $link-color: blue; $link-color ...
- Android(java)学习笔记197:ContentProvider使用之内容观察者02
下面通过3个应用程序之间的交互说明一下内容观察者: 一. 如下3个应用程序为相互交互的: 二.交互逻辑图: 三.具体代码: 1. 16_数据库工程: (1)数据库帮助类BankDBOpenHelp ...
- C#在sql中使用變量訪問Oracle數據庫
1.首先創建一個測試數據表 CREATE TABLE people ( SNO BYTE), SNAME BYTE), SSEX BYTE), SAGE number, SDEPT BYTE), BT ...
- c++ 模板template
1.函数模板的声明 声明形式 template<typename 数据类型参数标识符> <返回类型><函数名>(参数表) { 函数体 } 注: templa ...
- 常用的HTTP方法有哪些?
GET: 用于请求访问已经被URI(统一资源标识符)识别的资源,可以通过URL传参给服务器POST:用于传输数据给服务器,主要功能与GET方法类似,但一般推荐使用POST方式.PUT: 传输数据,报文 ...
- zabbix4.2学习笔记--监控nginx
图解一个客户端连接开源版本的Nginx情况 Accepts(接受).Handled(已处理).Requests(请求数)是一直在增加的计数器.Active(活跃).Waiting(等待).Readin ...
- Java中a=a++ 和 a=++a(转)
转自https://blog.csdn.net/lovepluto/article/details/81062176 如果问 a++ 和 ++a 的区别,估计很多都能回答上来.a++ 是先取 a 的值 ...
- librdkafka使用VS2015进行编译
抄了那么久的<kafka权威指南>,开始实操了,按照书本的介绍,kafka本身提供针对Java的原生API,其它语言如果需要使用kafka,那么就需要通过第三方库来做了,对了再书中一直提及 ...
- xtrbackup备份mysql
mysqldump备份方式是采用逻辑备份,但是它最大的缺陷就是备份和恢复速度慢对于一个小于50G的数据库而言,这个速度还是能接受的,但如果数据库非常大,那再使用mysqldump备份就不太适合了. x ...
- prop 和 attr 中一些羞羞的事情
引言 前几天做一个迷你京东小项目的时候涉及到一个全选的小功能,一开始用的是 attr,但是效果完全不是自己想要的,当商品按钮点击过一次后,attr就无法对其状态进行更改,最后谷歌了一番发现需要用 pr ...