本文整理汇总了TypeScript中boxed_value.box函数的典型用法代码示例。如果您正苦于以下问题:TypeScript box函数的具体用法?TypeScript box怎么用?TypeScript box使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了box函数的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: fetchStoredToken
export function fetchStoredToken(): AuthState | undefined {
try {
const v: AuthState = JSON.parse(localStorage.getItem(KEY) || "");
if (box(v).kind === "object") {
return v;
} else {
throw new Error("Expected object or undefined"); // unreachable?
}
} catch (error) {
return undefined;
}
}
示例2: get
export function get(): AuthState | undefined {
try {
let v: AuthState = JSON.parse(localStorage[KEY]);
if (box(v).kind === "object") {
return v;
} else {
throw new Error("Expected object or undefined");
}
} catch (error) {
return undefined;
}
}
示例3: safeStringFetch
export function safeStringFetch(obj: {}, key: string): string {
const boxed = box((obj as Dictionary<{}>)[key]);
switch (boxed.kind) {
case "undefined":
case "null":
return "";
case "number":
case "string":
return boxed.value.toString();
case "boolean":
return (boxed.value) ? "true" : "false";
default:
const msg = t(`Numbers strings and null only (got ${boxed.kind}).`);
throw new Error(msg);
}
}
示例4: box
parse: (__, val) => {
try {
const b = box(JSON.parse(val));
switch (b.kind) {
case "number":
return b.value;
case "boolean":
case "string":
return getSpecialValue(val);
default:
throw new Error("BAD DATA TYPE");
}
} catch (error) {
throw new Error(`An input from FarmWare caused a crash.
This is the value we got: ${val}
This is the error: ${error}
`);
}
}
示例5: isSafeError
export function isSafeError(x: SafeError | any): x is SafeError {
return !!(
(box(x).kind === "object") &&
(box(x.response).kind === "object") &&
(box(x.response.status).kind === "number"));
}