TypeScript 2.2: Dotted Properties and String Index Signatures
Before TypeScript 2.2, you were forced to use the []
notation if you wanted to access arbitrary properties of a type with a string index signature. You were not allowed to use the common .
notation:
interface Dictionary<T> {
[key: string]: T;
}
const portNumbers: Dictionary<number> = {};
// OK
portNumbers["http"] = 80;
// Error: Property 'http' does not exist on type 'Dictionary<number>'.
portNumbers.http = 80;
TypeScript 2.2 removes that restriction. You can now access properties using either bracket or dot notation without the compiler yelling at you. In many situations, there'll no longer be a need for unpleasant workarounds like this:
// Awkward!
(portNumbers as any).http = 80;
Note that the type must define an explicit string index signature in order for dotted property access to be type-correct for arbitrary properties. TypeScript 2.2 will therefore still give you a compile-time error for the following code:
const portNumbers = {};
// OK
portNumbers["http"] = 80;
// Error: Property 'http' does not exist on type '{}'.
portNumbers.http = 80;
It makes a lot of sense if you think about it: If TypeScript didn't give you an error for this code, there would be no protection against misspelled property names. You'll use dot notation most of the time when you access properties in JavaScript, but you can always fall back to bracket notation as an escape hatch.
With this loosened restriction, TypeScript makes another JavaScript idiom more natural to work with. This is especially helpful if you're migrating an existing JavaScript code base to TypeScript. Given proper string index signatures, you'll get fewer type errors in these cases, and you'll no longer need to annotate dotted property accesses with type annotations just to make the compiler happy.
This post is part of the TypeScript Evolution series:
- TypeScript 2.0: Non-Nullable Types
- TypeScript 2.0: Control Flow Based Type Analysis
- TypeScript 2.0: Acquiring Type Declaration Files
- TypeScript 2.0: Read-Only Properties
- TypeScript 2.0: Tagged Union Types
- TypeScript 2.0: More Literal Types
- TypeScript 2.0: The never Type
- TypeScript 2.0: Built-In Type Declarations
- TypeScript 2.1: async/await for ES3/ES5
- TypeScript 2.1: External Helpers Library
- TypeScript 2.1: Object Rest and Spread
- TypeScript 2.1: keyof and Lookup Types
- TypeScript 2.1: Mapped Types
- TypeScript 2.1: Improved Inference for Literal Types
- TypeScript 2.1: Literal Type Widening
- TypeScript 2.1: Untyped Imports
- TypeScript 2.2: The object Type
- TypeScript 2.2: Dotted Properties and String Index Signatures
- TypeScript 2.2: Null-Checking for Expression Operands
- TypeScript 2.2: Mixin Classes
- TypeScript 2.3: Generic Parameter Defaults
- TypeScript 2.3: The --strict Compiler Option
- TypeScript 2.3: Type-Checking JavaScript Files with --checkJs
- TypeScript 2.3: Downlevel Iteration for ES3/ES5
- TypeScript 2.4: String Enums
- TypeScript 2.4: Weak Type Detection
- TypeScript 2.4: Spelling Correction
- TypeScript 2.4: Dynamic import() Expressions
- TypeScript 2.5: Optional catch Binding
- TypeScript 2.6: JSX Fragment Syntax
- TypeScript 2.7: Numeric Separators
- TypeScript 2.7: Strict Property Initialization
- TypeScript 2.8: Per-File JSX Factories
- TypeScript 2.8: Conditional Types
- TypeScript 2.8: Mapped Type Modifiers