js2ts: util.ts 분리

This commit is contained in:
2021-08-21 20:45:10 +09:00
parent edbe8e3561
commit f23ebcdc2d
16 changed files with 56 additions and 86 deletions
+4
View File
@@ -0,0 +1,4 @@
export class NotNullExpected extends TypeError {
public name = 'NotNullExpected';
}
+1
View File
@@ -0,0 +1 @@
export type Nullable<T> = T | null | undefined;
+14
View File
@@ -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;
}
}
}
+9
View File
@@ -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;
}
+2 -1
View File
@@ -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 {
+10
View File
@@ -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;
}