69 lines
1.4 KiB
TypeScript
69 lines
1.4 KiB
TypeScript
type BeeRole = 'worker'|'queen'|'guard';
|
|
type Months = number;
|
|
type Bee = {
|
|
role: BeeRole,
|
|
name: string,
|
|
age: Months,
|
|
};
|
|
const bee: Bee = {
|
|
role: "guard",
|
|
name: 'Juergen',
|
|
age: 2,
|
|
}
|
|
|
|
const userNames = {
|
|
'652a4707-3043-4ca9-b417-24feac0f5953': 'Bernhard',
|
|
'017daef7-e1f1-4160-81ff-e5b17e86fc6b': 'Frieda',
|
|
};
|
|
|
|
type UserNameDictionary = Record<string, string>;
|
|
|
|
type Renderable = {
|
|
render: () => void,
|
|
}
|
|
type Updateable = {
|
|
update: () => void,
|
|
}
|
|
type GameObject = Renderable & Updateable;
|
|
const gameObject: GameObject = {
|
|
render: () => {},
|
|
update: () => {},
|
|
}
|
|
function fetch(): FetchResult {
|
|
return Math.random() > 0.5
|
|
? {msg: 'bla', data: {}}
|
|
: {msg: 'bla', trace: []};
|
|
}
|
|
|
|
type NetworkPacket = {
|
|
data: Record<string, string>,
|
|
msg: string,
|
|
};
|
|
type Exception = {
|
|
trace: string[],
|
|
msg: string,
|
|
}
|
|
type FetchResult = NetworkPacket | Exception;
|
|
const fetchResult: FetchResult = fetch();
|
|
console.log(fetchResult.msg);
|
|
if ('trace' in fetchResult) {
|
|
console.log(fetchResult.trace);
|
|
}
|
|
if ('data' in fetchResult) {
|
|
console.log(fetchResult.data);
|
|
}
|
|
|
|
/** @see {isDuckSort} ts-auto-guard:type-guard */
|
|
export type DuckSort = {
|
|
name: string,
|
|
id: number,
|
|
species: string,
|
|
habitat: string[],
|
|
}
|
|
|
|
function inferBroken(str: string) {
|
|
if (['Bernd', 'Margeret'].includes(str)) {
|
|
const narrowed: 'Bernd'|'Margeret' = str;
|
|
}
|
|
}
|