forked from sindresorhus/file-type
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.test-d.ts
54 lines (43 loc) · 1.83 KB
/
index.test-d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import * as fs from 'fs';
import {expectType} from 'tsd';
import FileType = require('.');
import {FileTypeResult, ReadableStreamWithFileType, FileExtension, MimeType} from '.';
import FileTypeBrowser = require('./browser');
import {FileTypeResult as FileTypeResultBrowser} from './browser';
expectType<Promise<FileTypeResult | undefined>>(FileType.fromBuffer(Buffer.from([0xff, 0xd8, 0xff])));
expectType<Promise<FileTypeResult | undefined>>(FileType.fromBuffer(new Uint8Array([0xff, 0xd8, 0xff])));
expectType<Promise<FileTypeResult | undefined>>(FileType.fromBuffer(new ArrayBuffer(42)));
(async () => {
const result = await FileType.fromBuffer(Buffer.from([0xff, 0xd8, 0xff]));
if (result !== undefined) {
expectType<FileExtension>(result.ext);
expectType<MimeType>(result.mime);
}
})();
(async () => {
expectType<FileTypeResult | undefined>(await FileType.fromFile('myFile'));
const result = await FileType.fromFile('myFile');
if (result !== undefined) {
expectType<FileExtension>(result.ext);
expectType<MimeType>(result.mime);
}
})();
(async () => {
const stream = fs.createReadStream('myFile');
expectType<FileTypeResult | undefined>(await FileType.fromStream(stream));
const result = await FileType.fromStream(stream);
if (result !== undefined) {
expectType<FileExtension>(result.ext);
expectType<MimeType>(result.mime);
}
})();
expectType<Set<FileType.FileExtension>>(FileType.extensions);
expectType<readonly FileType.MimeType[]>(FileType.mimeTypes);
const readableStream = fs.createReadStream('file.png');
const streamWithFileType = FileType.stream(readableStream);
expectType<Promise<ReadableStreamWithFileType>>(streamWithFileType);
(async () => {
expectType<FileTypeResult | undefined>((await streamWithFileType).fileType);
})();
// Browser
expectType<Promise<FileTypeResult | undefined>>(FileTypeBrowser.fromBlob(new Blob()));