[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之间的兼容性已经渐渐的被各种技术解决了,比如经典 ...
随机推荐
- node.js入门之三
Node.js REPL(交互式解释器) Node.js REPL(Read Eval Print Loop:交互式解释器) 表示一个电脑的环境,类似 Window 系统的终端或 Unix/Linux ...
- nutz配置druid监控
druid 提供了一个web端的监控页面, 搭建起来不算麻烦, 建议添加. 打开web.xml, 在nutz的filter之前, 加入Web监控的配置 <filter> <filte ...
- Android(java)学习笔记187:多媒体之SurfaceView
1. SurfaceView: 完成单位时间内界面的快速切换(游戏界面流畅感). 我们之前知道一般的View,只能在主线程里面显示,主线程中更新UI.但是SurfaceView可以在子线程中里 ...
- 09Windows编程
Windows编程 2.1 窗口 Windows应用程序一般都有一个窗口,窗口是运行程序与外界交换信息的界面.一个典型的窗口包括标题栏.最小化按钮.最大/还原按钮.关闭按钮.系统菜单图标.菜 ...
- python中的next()以及iter()函数
我们首先要知道什么是可迭代的对象(可以用for循环的对象)Iterable: 一类:list,tuple,dict,set,str 二类:generator,包含生成器和带yield的generato ...
- es6 day01
es6语法必须加‘use strict’ 'use strict' //预解释 变量提升 先看下边例子来感受下let的用法与特点 /* console.log(a);//undefined 只声明未定 ...
- 51nod 1013 3的幂的和 - 快速幂&除法取模
题目地址:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1013 Konwledge Point: 快速幂:https:/ ...
- 笔试算法题(44):简介 - 动态规划(Dynamic Programming)
议题:动态规划(Dynamic Programming) 分析: DP主要用于解决包含重叠子问题(Overlapping Subproblems)的最优化问题,其基本策略是将原问题分解为相似的子问题, ...
- 【project】十次方-01
前言 项目介绍 系统分为3大部分:微服务.网站前台.网站管理后台:功能模块分为:问答.招聘.交友中心等 该项目融合了Docker容器化部署.第三方登陆.SpringBoot.SpringCloud.S ...
- laravel学习笔记2--表单
一.Controller 1.Request 1.1.取值:input // 1.取值 echo $request->input('name'); // 2.取不到值时打印默认值 echo $r ...