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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [TypeScript] The Basics of Generics in TypeScript

    It can be painful to write the same function repeatedly with different types. Typescript generics al ...

  4. electron教程(番外篇二): 使用TypeScript版本的electron, VSCode调试TypeScript, TS版本的ESLint

    我的electron教程系列 electron教程(一): electron的安装和项目的创建 electron教程(番外篇一): 开发环境及插件, VSCode调试, ESLint + Google ...

  5. Java中的原始类型(Primitive Types)与引用类型(Reference Values)

    Java虚拟机可以处理的类型有两种,一种是原始类型(Primitive Types),一种是引用类型(Reference Types). 与之对应,也存在有原始值(Primitive Values)和 ...

  6. 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 ...

  7. Implement Hash Map Using Primitive Types

    A small coding test that I encountered today. Question Using only primitive types, implement a fixed ...

  8. C# 01 Primitive Types and Expressions

    Class Data or Attributes state of the application Methods or Functions have behavior Namespace is a ...

  9. 玩转TypeScript(引言&文章目录) --初看TypeScript.

    JavaScript过去一直被当作一种玩具语言存在,直到2005年以后,这门语言又开始活跃并可以说是火爆,而且随着浏览器版本的不断升级和完善,各种DOM之间的兼容性已经渐渐的被各种技术解决了,比如经典 ...

随机推荐

  1. 【译】x86程序员手册36-9.9异常汇总

    9.9 Exception Summary 异常汇总 Table 9-6 summarizes the exceptions recognized by the 386. Table 9-6. Exc ...

  2. 基于ANGULAR.JS的下一代WEB应用开发-01-yeoman

    Angularjs 个人认为这是一款很好的框架!它将我们从AJAX应用的开发中解救了出来!嗯....废话就说道这里下面我们开始把! 首先我们必须了解一些核心的概念: 客户端模版 MVC 数据绑定 依赖 ...

  3. 类的封装,property特性,类与对象的绑定方法和非绑定方法,

    类的封装 就是把数据或者方法封装起来 为什么要封装 封装数据的主要原因是:保护隐私 封装方法的主要原因是:隔离复杂度(快门就是傻瓜相机为傻瓜们提供的方法,该方法将内部复杂的照相功能都隐藏起来了,比如你 ...

  4. 计算机内存数据存储基本原理----寄存器和RAM的电路基础

    计算机里存储数据主要有这几个部件:CPU里的寄存器和缓存.内存(内存条)和磁盘,这里我们主要简单讲下寄存器和内存条的基础实现电路. 在前面的文章<CPU怎么计算1+1----CPU计算的电路基础 ...

  5. 【转】c语言中的定义和声明

    1. 变量的定义.声明 变量的声明有两种情况: 一种是需要建立存储空间的.例如:int  a.在声明的时候就已经建立了存储空间.这种声明是"定义性声明(defining declaratio ...

  6. A7. JVM 垃圾回收收集器(GC 收集器)

    [概述] 如果说收集算法是内存回收的方法论,那么垃圾收集器就是内存回收的具体实现.Java 虚拟机规范中对垃圾收集器应该如何实现没有任何规定,因此不同的厂商.不同版本的虚拟机所提供的垃圾处理器都可能会 ...

  7. ecshop 修改支持php7 方案

    修改方法 http://jsb.php-php.com/2016/05/472/ 修改数据库配置 data/config.php

  8. 集训第六周 数学概念与方法 概率 数论 最大公约数 G题

    Description There is a hill with n holes around. The holes are signed from 0 to n-1. A rabbit must h ...

  9. jz2440烧写方法笔记

    1,jz2440用dnw烧写普通程序的步骤是: ①选择将uboot烧写到nor flash或者是nand flash中 ②打开dnw,选择要烧写的程序,注意不是uboot,是你要烧写的程序,他将所有的 ...

  10. Android 笔记一:线性布局

    建立布局 新建项目后,在如图路径下新建xml文件可以开始编辑 weight的使用 android:layout_width="0dp",或android:layout_width= ...