mirror of
https://github.com/hanzoai/tsgo.git
synced 2026-10-30 19:37:15 +00:00
34 lines
1.0 KiB
TypeScript
34 lines
1.0 KiB
TypeScript
//// [tests/cases/conformance/controlFlow/controlFlowInstanceOfGuardPrimitives.ts] ////
|
|
|
|
//// [controlFlowInstanceOfGuardPrimitives.ts]
|
|
function distinguish(thing: string | number | Date) {
|
|
if (thing instanceof Object) {
|
|
console.log("Aha!! It's a Date in " + thing.getFullYear());
|
|
} else if (typeof thing === 'string') {
|
|
console.log("Aha!! It's a string of length " + thing.length);
|
|
} else {
|
|
console.log("Aha!! It's the number " + thing.toPrecision(3));
|
|
}
|
|
}
|
|
|
|
distinguish(new Date());
|
|
distinguish("beef");
|
|
distinguish(3.14159265);
|
|
|
|
//// [controlFlowInstanceOfGuardPrimitives.js]
|
|
"use strict";
|
|
function distinguish(thing) {
|
|
if (thing instanceof Object) {
|
|
console.log("Aha!! It's a Date in " + thing.getFullYear());
|
|
}
|
|
else if (typeof thing === 'string') {
|
|
console.log("Aha!! It's a string of length " + thing.length);
|
|
}
|
|
else {
|
|
console.log("Aha!! It's the number " + thing.toPrecision(3));
|
|
}
|
|
}
|
|
distinguish(new Date());
|
|
distinguish("beef");
|
|
distinguish(3.14159265);
|