Typescript datatypes: Difference between revisions

From wikinotes
(Created page with "= Primitives = <blockquote> <syntaxhighlight lang="typescript"> string number boolean </syntaxhighlight> </blockquote><!-- Primitives -->")
 
No edit summary
Line 7: Line 7:
</syntaxhighlight>
</syntaxhighlight>
</blockquote><!-- Primitives -->
</blockquote><!-- Primitives -->
= Object Types =
<blockquote>
You can define sort-of anonymous interfaces, based on the properties you require.
<syntaxhighlight lang="typescript">
function volume(coords: { x: number; y: number; z: number; }): number {
    // ...
}
</syntaxhighlight>
</blockquote><!-- Object Types -->
= Unions =
<blockquote>
A param accepting a variety of types.
<syntaxhighlight lang="typescript">
function print(msg: number | string | boolean): null {
    console.log(msg)
}
</syntaxhighlight>
</blockquote><!-- Unions -->

Revision as of 03:16, 11 December 2022

Primitives

string
number
boolean

Object Types

You can define sort-of anonymous interfaces, based on the properties you require.

function volume(coords: { x: number; y: number; z: number; }): number {
    // ...
}

Unions

A param accepting a variety of types.

function print(msg: number | string | boolean): null {
    console.log(msg)
}