Relaxed Rules Between Optional Properties and String Index Signatures
TypeScript 5.4 relaxes the rules governing the interaction between optional properties and string index signatures, providing more flexibility in type definitions.
Syntax:
interface MyObj {
[key: string]: number;
optionalProp?: string;
}
Example: Defining an Object with Optional Property in this example, we define an interface MyObj with a string index signature and an optional property.
interface MyObj {
[key: string]: number;
optionalProp?: string;
}
const obj: MyObj = {
optionalProp: 'gfg',
key: 22,
};
console.log(obj.optionalProp);
console.log(obj['key']);
Output
gfg
22
What’s new in TypeScript 5.4 ?
TypeScript 5.4 brings forth several new capabilities and refinements, aiming to enhance developer productivity and code quality. Some of the new capabilities that are added or Introduced in TypeScript 5.4 are as follows:
Table of Content
- Variadic Tuple Types
- Class Property Inference from Constructors
- Local Assignment Discouragement
- Relaxed Rules Between Optional Properties and String Index Signatures