type Mammal = {
name: string;
lungCapacity: number;
};
type OceanDweller = {
name: string;
favoriteOcean: string;
};
type OceanDwellingMammal = Mammal & OceanDweller;
const aardvark: Mammal = {
name: "aardvark",
lungCapacity: 1,
};
const tuna: OceanDweller = {
name: "tuna",
favoriteOcean: "pacific",
};
const dolphin = {
name: "dolphin",
lungCapacity: 4,
favoriteOcean: "atlantic",
};
type SuccessfulString = {
status: "success";
value: string;
};
type Failure = {
status: "fail";
err: Error;
};
type StringResult = SuccessfulString | Failure;
function generateDateStringOnlyIfEven(): StringResult {
const now = new Date();
if (now.getMilliseconds() % 2 === 0) {
return {
status: "success",
value: now.toISOString(),
};
} else {
return {
status: "fail",
err: new Error("not an even millisecond"),
};
}
}
const s = generateDateStringOnlyIfEven();
if (s.status === "success") {
console.info(s.value);
} else {
console.error(s.err);
}