Subido por hqidufgdg

TypeScript Interfaces

Anuncio
TypeScript Common Syntax
Cheat Sheet
Optionally take properties from
Overloads
existing interface or type
A callable interface can have multiple definitions Interface
for different sets of parameters
interface JSONResponse extends Response, HTTPAble {
interface Expect {
version: number;
h
(matcher:
Key points
/** In bytes */
payloadSize: number;
Used to describe the shape of
objects, and can be extended by
others.
g
(matc er: boolean): strin
JSDoc comment attached to show in editors
string): boolean;
}
This property might not be on the object
outOfStock?: boolean;
These are two ways to describe a
property which is a function
Almost everything in JavaScript is
an object and interface is built
update: (retryTimes: number) => void;
to match their runtime behavior.
update(retryTimes: number): void; Get & Set
Objects
can have custom getters or setters
interface Ruler {
You can call this object via
(): JSONResponse
Built-in
()
r
get size(): numbe
- ( functions
in JS are objects which can be called )
set size(value: number
Type Primitives
new(s: string): JSONResponse;
You can use
new
on the object
undefined, null, any,
Usage
[key: string]: number;
unknown, never, void,
Any property not described already is assumed
to exist, and all properties must be numbers
bigint, symbol
readonly body: string;
Built-in
JS
Objects
string);
this interface describes
boolean, string, number,
Common
}
|
const r: Ruler =
.
r.size
r size =
}
Tells TypeScript that a property
=
12
"36"
...
can not be changed
Date, Error, Array, Map,
Set, Regexp, Promise
Type Literals
Generics
Object:
{
field: string
Extension via merging
}
Function:
(arg: number) => string Avoid
w fields to the type definition.
C
interface API all {
C
data: Response
}
data: Response
}
U
sed here
C
C
const api: API all<Artwork all> =
.
//
C
error?: Error
interface API all {
Usage
api data
will
add ne
interface API all<Response> {
Tuple:
[string, number]
Interfaces are merged, so multiple declarations
Declare a type which can change in your interface
Arrays:
string[] or Array<string>
Type parameter
...
Artwork
}
Sets a constraint on the type
which means only types with a
Object, String, Number, Boolean
You can constrain what types are accepted into the generic
parameter via the extends keyword.
C
interface API all<Response extends
{
‘
’
status property can be used
status: number }> {
data: Response
Class conformance
You can ensure a class conforms to an interface via implements:
sync(): void
}
class Account implements Syncable
{
interface Syncable
{
}
C
C
const api: API all<Artwork all> =
.
.
api data status
...
...
}
Descargar