Created
May 24, 2020 11:40
-
-
Save kdy1/2dbb1efc34e3ae2128dcecd6fb5822da to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// @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