filmov
tv
TypeScript Tutorials #20 - Syntax around interfaces
![preview_player](https://i.ytimg.com/vi/zwVz32dnlcA/maxresdefault.jpg)
Показать описание
#Interface
One of TypeScript’s core principles is that type checking focuses on the shape that values have. This is sometimes called “duck typing” or “structural subtyping”. In TypeScript, interfaces fill the role of naming these types, and are a powerful way of defining contracts within your code as well as contracts with code outside of your project.
Not all properties of an interface may be required. Some exist under certain conditions or may not be there at all. These optional properties are popular when creating patterns like “option bags” where you pass an object to a function that only has a couple of properties filled in.
Interfaces with optional properties are written similar to other interfaces, with each optional property denoted by a ? at the end of the property name in the declaration. The advantage of optional properties is that you can describe these possibly available properties while still also preventing the use of properties that are not part of the interface.
Some properties should only be modifiable when an object is first created. You can specify this by putting "readonly" before the name of the property:
interface Point {
readonly x: number;
readonly y: number;
}
✅ Let's connect:
Twitter - @AnshulLaddha3
Instagram - /anshul_laddha
One of TypeScript’s core principles is that type checking focuses on the shape that values have. This is sometimes called “duck typing” or “structural subtyping”. In TypeScript, interfaces fill the role of naming these types, and are a powerful way of defining contracts within your code as well as contracts with code outside of your project.
Not all properties of an interface may be required. Some exist under certain conditions or may not be there at all. These optional properties are popular when creating patterns like “option bags” where you pass an object to a function that only has a couple of properties filled in.
Interfaces with optional properties are written similar to other interfaces, with each optional property denoted by a ? at the end of the property name in the declaration. The advantage of optional properties is that you can describe these possibly available properties while still also preventing the use of properties that are not part of the interface.
Some properties should only be modifiable when an object is first created. You can specify this by putting "readonly" before the name of the property:
interface Point {
readonly x: number;
readonly y: number;
}
✅ Let's connect:
Twitter - @AnshulLaddha3
Instagram - /anshul_laddha