Skip to content

Commit

Permalink
More TypeScript type fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jan 7, 2020
1 parent 2c86443 commit e209647
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
4 changes: 2 additions & 2 deletions browser.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const url = 'https://upload.wikimedia.org/wikipedia/en/a/a9/Example.jpg';
})();
```
*/
export declare function fromStream(stream: ReadableStream): Promise<core.FileTypeResult>;
export declare function fromStream(stream: ReadableStream): Promise<core.FileTypeResult | undefined>;

/**
Determine file type from a [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob).
Expand All @@ -41,7 +41,7 @@ import FileType = require('file-type/browser');
})();
```
*/
export declare function fromBlob(blob: Blob): Promise<core.FileTypeResult>;
export declare function fromBlob(blob: Blob): Promise<core.FileTypeResult | undefined>;

export {
fromBuffer,
Expand Down
6 changes: 3 additions & 3 deletions core.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,12 +245,12 @@ declare namespace core {
/**
One of the supported [file types](https://github.com/sindresorhus/file-type#supported-file-types).
*/
ext: FileExtension;
readonly ext: FileExtension;

/**
The detected [MIME type](https://en.wikipedia.org/wiki/Internet_media_type).
*/
mime: MimeType;
readonly mime: MimeType;
}

type ReadableStreamWithFileType = ReadableStream & {
Expand Down Expand Up @@ -306,7 +306,7 @@ declare namespace core {
@param tokenizer - File source implementing the tokenizer interface.
@returns The detected file type and MIME type, or `undefined` when there is no match.
*/
function fromTokenizer(tokenizer: ITokenizer): Promise<core.FileTypeResult>;
function fromTokenizer(tokenizer: ITokenizer): Promise<core.FileTypeResult | undefined>;

/**
Deprecated: The minimum amount of bytes needed to detect a file type. Currently, it's 4100 bytes, but it can change, so don't hard-code it.
Expand Down
25 changes: 15 additions & 10 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import * as fs from 'fs';
import {expectType} from 'tsd';
import FileType = require('.');
import {FileTypeResult, ReadableStreamWithFileType, FileExtension} from '.';
import {FileTypeResult, ReadableStreamWithFileType, FileExtension, MimeType} from '.';
import FileTypeBrowser = require('./browser');
import {FileTypeResult as FileTypeResultBrowser} from './browser';

expectType<Promise<FileTypeResult | undefined>>(FileType.fromBuffer(new Buffer([0xff, 0xd8, 0xff])));
expectType<Promise<FileTypeResult | undefined>>(FileType.fromBuffer(new Uint8Array([0xff, 0xd8, 0xff])));
Expand All @@ -11,17 +13,17 @@ expectType<Promise<FileTypeResult | undefined>>(FileType.fromBuffer(new ArrayBuf
const result = await FileType.fromBuffer(new Buffer([0xff, 0xd8, 0xff]));
if (result !== undefined) {
expectType<FileExtension>(result.ext);
expectType<string>(result.mime);
expectType<MimeType>(result.mime);
}
})();

(async () => {
expectType<FileTypeResult | undefined>(await FileType.fromFile('myFile'));

const fileRes = await FileType.fromFile('myFile');
if (fileRes !== undefined) {
expectType<FileExtension>(fileRes.ext);
expectType<string>(fileRes.mime);
const result = await FileType.fromFile('myFile');
if (result !== undefined) {
expectType<FileExtension>(result.ext);
expectType<MimeType>(result.mime);
}
})();

Expand All @@ -30,10 +32,10 @@ expectType<Promise<FileTypeResult | undefined>>(FileType.fromBuffer(new ArrayBuf

expectType<FileTypeResult | undefined>(await FileType.fromStream(stream));

const fileRes = await FileType.fromStream(stream);
if (fileRes !== undefined) {
expectType<FileExtension>(fileRes.ext);
expectType<string>(fileRes.mime);
const result = await FileType.fromStream(stream);
if (result !== undefined) {
expectType<FileExtension>(result.ext);
expectType<MimeType>(result.mime);
}
})();

Expand All @@ -50,3 +52,6 @@ expectType<Promise<ReadableStreamWithFileType>>(streamWithFileType);
(async () => {
expectType<FileTypeResult | undefined>((await streamWithFileType).fileType);
})();

// Browser
expectType<Promise<FileTypeResult | undefined>>(FileTypeBrowser.fromBlob(new Blob()));
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@
"noop-stream": "^0.1.0",
"pify": "^4.0.1",
"read-chunk": "^3.2.0",
"tsd": "^0.7.0",
"tsd": "^0.11.0",
"xo": "^0.25.3"
},
"dependencies": {
Expand Down

0 comments on commit e209647

Please sign in to comment.