Skip to content

Instantly share code, notes, and snippets.

@kdy1
Created May 24, 2020 11:40
Show Gist options
  • Save kdy1/2dbb1efc34e3ae2128dcecd6fb5822da to your computer and use it in GitHub Desktop.
Save kdy1/2dbb1efc34e3ae2128dcecd6fb5822da to your computer and use it in GitHub Desktop.
declare type PrimitiveName = 'string' | 'number' | 'boolean';
declare const string: "string";
declare const number: "number";
declare const boolean: "boolean";
declare const stringOrBoolean: "string" | "boolean";
declare const stringOrBooleanOrNumber: "string" | "number" | "boolean";
type PrimitiveName = 'string' | 'number' | 'boolean';
const string: "string" = "string";
const number: "number" = "number";
const boolean: "boolean" = "boolean";
const stringOrBoolean = string || boolean;
const stringOrBooleanOrNumber = stringOrBoolean || number;
declare type PrimitiveName = 'string' | 'number' | 'boolean';
declare function getFalsyPrimitive(x: "string"): string;
declare function getFalsyPrimitive(x: "number"): number;
declare function getFalsyPrimitive(x: "boolean"): boolean;
declare function getFalsyPrimitive(x: "boolean" | "string"): boolean | string;
declare function getFalsyPrimitive(x: "boolean" | "number"): boolean | number;
declare function getFalsyPrimitive(x: "number" | "string"): number | string;
declare function getFalsyPrimitive(x: "number" | "string" | "boolean"): number | string | boolean;
declare const string: "string";
declare const number: "number";
declare const boolean: "boolean";
declare const stringOrNumber: "string" | "number";
declare const stringOrBoolean: "string" | "boolean";
declare const booleanOrNumber: "number" | "boolean";
declare const stringOrBooleanOrNumber: PrimitiveName;
// @declaration: true
type PrimitiveName = 'string' | 'number' | 'boolean';
function getFalsyPrimitive(x: "string"): string;
function getFalsyPrimitive(x: "number"): number;
function getFalsyPrimitive(x: "boolean"): boolean;
function getFalsyPrimitive(x: "boolean" | "string"): boolean | string;
function getFalsyPrimitive(x: "boolean" | "number"): boolean | number;
function getFalsyPrimitive(x: "number" | "string"): number | string;
function getFalsyPrimitive(x: "number" | "string" | "boolean"): number | string | boolean;
function getFalsyPrimitive(x: PrimitiveName): number | string | boolean {
if (x === "string") {
return "";
}
if (x === "number") {
return 0;
}
if (x === "boolean") {
return false;
}
// Should be unreachable.
throw "Invalid value";
}
const string: "string" = "string";
const number: "number" = "number";
const boolean: "boolean" = "boolean";
const stringOrNumber = string || number;
const stringOrBoolean = string || boolean;
const booleanOrNumber = number || boolean;
const stringOrBooleanOrNumber = stringOrBoolean || number;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment