js2ts: util.ts 분리
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
|
||||
export class NotNullExpected extends TypeError {
|
||||
public name = 'NotNullExpected';
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export type Nullable<T> = T | null | undefined;
|
||||
@@ -0,0 +1,14 @@
|
||||
export class RuntimeError extends Error {
|
||||
public name = 'RuntimeError';
|
||||
constructor(public message: string = '') {
|
||||
super(message);
|
||||
}
|
||||
toString(): string {
|
||||
if (this.message) {
|
||||
return this.name + ': ' + this.message;
|
||||
}
|
||||
else {
|
||||
return this.name;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
import { Nullable } from "./Nullable";
|
||||
import { NotNullExpected } from "./NotNullExpected";
|
||||
|
||||
export function unwrap<T>(result: Nullable<T>): T {
|
||||
if (result === null || result === undefined) {
|
||||
throw new NotNullExpected();
|
||||
}
|
||||
return result;
|
||||
}
|
||||
@@ -1,4 +1,5 @@
|
||||
import { Nullable, NotNullExpected } from "../util";
|
||||
import { Nullable } from "./Nullable";
|
||||
import { NotNullExpected } from "./NotNullExpected";
|
||||
|
||||
|
||||
export function unwrap_any<T>(result: Nullable<unknown>): T {
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
import { Nullable } from "./Nullable";
|
||||
|
||||
type ErrType<T> = { new(msg?: string): T }
|
||||
|
||||
export function unwrap_err<T, ErrT extends Error>(result: Nullable<T>, errType: ErrType<ErrT>, errMsg?: string): T {
|
||||
if (result === null || result === undefined) {
|
||||
throw new errType(errMsg);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
Reference in New Issue
Block a user