0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

TypeScriptでPromiseの型が複数の時の書き方

Posted at

TypeScriptでPromiseの型が複数の時の書き方

表題の件、検索しても有益な情報が見つからなかったので記載
単純にPromiseの型定義でパイプでつなぐだけ

確認環境

Typescript 3.8.3
MacOS 10.14.6

sample

以下の例だとFlileReaderからreadAsTextで読み込み完了したとき、resultの型である
string|ArrayBuffer|nullをPromiseに定義するだけ

export async function readFile(file: File, ):Promise<string | ArrayBuffer | null> {
    return new Promise<string | ArrayBuffer | null>((resolve, reject) => {
        const reader: FileReader = new FileReader();
        reader.onloadend = () => resolve(reader.result);
        reader.readAsText(file);
    });
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?