PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, July 9, 2022

[FIXED] What is the "type" reserved word in TypeScript?

 July 09, 2022     keyword, reserved-words, typescript     No comments   

Issue

I just noticed when trying to create an interface in TypeScript that "type" is either a keyword or a reserved word. When creating the following interface, for example, "type" is shown in blue in Visual Studio 2013 with TypeScript 1.4:

interface IExampleInterface {
    type: string;
}

Let's say that you then try to implement the interface in a class, like this:

class ExampleClass implements IExampleInterface {
    public type: string;

    constructor() {
        this.type = "Example";
    }
}

In the first line of the class, as you type (sorry) the word "type" in order to implement the property required by the interface, IntelliSense appears with "type" having the same icon as other keywords like "typeof" or "new".

I've had a look around, and could find this GitHub issue which lists "type" as a "strict mode reserved word" in TypeScript, but I have not found any further information about what its purpose actually is.

I suspect I'm having a brain fart and this is something obvious I should already know, but what is the "type" reserved word in TypeScript for?


Solution

It's used for "type aliases". For example:

type StringOrNumber = string | number;
type DictionaryOfStringAndPerson = Dictionary<string, Person>;

Reference: (edit: removed outdated link) TypeScript Specification v1.5 (section 3.9, "Type Aliases", pages 46 & 47)

Update: (edit: removed outdated link) Now on section 3.10 of the 1.8 spec. Thanks @RandallFlagg for the updated spec and link

Update: (edit: deprecated link) TypeScript Handbook, search "Type Aliases" can get you to the corresponding section.

Update: Now it's here in the TypeScript Handbook.



Answered By - Jcl
Answer Checked By - Robin (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing