Typescript datatypes: Difference between revisions
From wikinotes
No edit summary |
No edit summary |
||
Line 8: | Line 8: | ||
</blockquote><!-- Primitives --> | </blockquote><!-- Primitives --> | ||
= | = Collections = | ||
<blockquote> | |||
== Arrays == | |||
<blockquote> | |||
<syntaxhighlight lang="typescript"> | |||
function print(names: string[]): null { | |||
console.log(names); | |||
} | |||
</syntaxhighlight> | |||
</blockquote><!-- Arrays --> | |||
== Sets == | |||
<blockquote> | |||
</blockquote><!-- Sets --> | |||
== Objects == | |||
<blockquote> | <blockquote> | ||
You can define sort-of anonymous interfaces, based on the properties you require. | You can define sort-of anonymous interfaces, based on the properties you require. | ||
Line 16: | Line 32: | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- | </blockquote><!-- Objects --> | ||
== Maps == | |||
<blockquote> | |||
</blockquote><!-- Maps --> | |||
</blockquote><!-- Collections --> | |||
= Typing = | |||
<blockquote> | |||
== Type Aliases == | |||
<blockquote> | |||
<syntaxhighlight lang="typescript"> | |||
type Point = { | |||
x: number | |||
y: number | |||
} | |||
function area(pt: Point): number { | |||
return pt.x * pt.y; | |||
} | |||
</syntaxhighlight> | |||
</blockquote><!-- Type Aliases --> | |||
== Any == | |||
<blockquote> | |||
<code>any</code> can refer to any object. | |||
</blockquote><!-- Any --> | |||
= Unions = | == Unions == | ||
<blockquote> | <blockquote> | ||
A param accepting a variety of types. | A param accepting a variety of types. | ||
Line 27: | Line 70: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- Unions --> | </blockquote><!-- Unions --> | ||
</blockquote><!-- Typing --> |
Latest revision as of 03:21, 11 December 2022
Primitives
string number boolean
Collections
Arrays
function print(names: string[]): null { console.log(names); }Sets
Objects
You can define sort-of anonymous interfaces, based on the properties you require.
function volume(coords: { x: number; y: number; z: number; }): number { // ... }Maps
Typing
Type Aliases
type Point = { x: number y: number } function area(pt: Point): number { return pt.x * pt.y; }Any
any
can refer to any object.Unions
A param accepting a variety of types.
function print(msg: number | string | boolean): null { console.log(msg) }