Skip to main content

interfaceType to define Object shape & behavior blueprint

use Interface to Type Object as either Record or Dictionary

Record

Object with fixed number of known named properties& any Type before Compile Time

Dictionary

Object with ASU arbitrary # properties with same type & unknown names (at Compile Time)

Object Optional Property

?: Type Annotation to mark optional object property

!snippet Object Method Type Annotation

interface coords {
x: number;
y: number;
distance(pt: Point): number;
}

interface will structurally match Object (not nominally) that have appropriate structure

interface allow code reuse in Function Signature for matching Parameter

can be used to define instructions on how to be mappable to method

can act as instructions for how other classes can qualify as args for a class method

TS makes an implicit check to see if params satisfy interface

implements?

_!snippet Typed Interface_

References

  1. What is the difference between a [Alias](alias_compound) vs [**_interface_**](interface_typed-object)? ↔ Only interfaces are extensible unlike types.

  2. !snippet [**_interface_**](interface_typed-object) with [readonly](readonly) prop

interface MyInterface {
readonly prop: number;
}