Incomplete filesystem declarations
Please consider updating declarations following my updates. You might want to update existing declaration or add new ones:
FileSystem related:
type PermissionOptions_ = {
readonly mode:"readwrite";
}
type FileSystemHandle_ = FileSystemDirectoryHandle | FileSystemFileHandle;
// https://developer.mozilla.org/en-US/docs/Web/API/FileSystemHandle
interface FileSystemDirectoryHandle {
readonly values:() => FileSystemHandle_[];
readonly queryPermission:(options:PermissionOptions_) => Promise<"granted" | "prompt" | unknown>;
readonly requestPermission:(options:PermissionOptions_) => Promise<"granted" | unknown>;
}
// https://developer.mozilla.org/en-US/docs/Web/API/FileSystemFileHandle
interface FileSystemFileHandle {
// already available in chrome
// https://github.com/whatwg/fs/pull/10#issuecomment-1385992738
readonly move:(newName:string) => Promise<void>;
}
// https://wicg.github.io/file-system-access/#enumdef-wellknowndirectory
type WellKnownDirectory = "desktop" | "documents" | "downloads" | "music" | "pictures" | "videos";
// https://wicg.github.io/file-system-access/#api-filepickeroptions-starting-directory
// https://wicg.github.io/file-system-access/#api-showdirectorypicker
type StartingDirectory = {
readonly id?:string; // If id’s length is more than 32, throw a TypeError.
readonly startIn?:FileSystemHandle | WellKnownDirectory;
}
type FilePickerOptionsType = {
readonly description?:string;
readonly accept:Record<string, ReadonlyArray<string>>;
}
// https://developer.mozilla.org/en-US/docs/Web/API/window/showSaveFilePicker
type SaveFilePickerOptions = {
readonly excludeAcceptAllOption?:boolean;
// https://github.com/WICG/file-system-access/blob/main/SuggestedNameAndDir.md
readonly startIn?:"desktop" | "documents" | "downloads" | "music" | "pictures" | "videos";
// https://github.com/WICG/file-system-access/blob/main/SuggestedNameAndDir.md#interaction-of-suggestedname-and-accepted-file-types
readonly suggestedName?:string;
readonly types?:FilePickerOptionsType[];
}
// https://developer.mozilla.org/en-US/docs/Web/API/window/showOpenFilePicker
// https://wicg.github.io/file-system-access/#api-filepickeroptions
type OpenFilePickerOptions = StartingDirectory & {
readonly multiple?:boolean;
readonly excludeAcceptAllOption?:boolean;
readonly types?:FilePickerOptionsType[];
}
const showOpenFilePicker:(options?:OpenFilePickerOptions) => Promise<(FileSystemFileHandle | FileSystemDirectoryHandle)[]>;
const showDirectoryPicker:(options?:StartingDirectory) => Promise<FileSystemDirectoryHandle>;
const showSaveFilePicker:(options?:SaveFilePickerOptions) => Promise<FileSystemFileHandle>;
interface DataTransferItem {
getAsFileSystemHandle():Promise<FileSystemHandle_>;
}
Notice I am using FileSystemHandle_ b/c its provides better guarding in code compared to FileSystemHandle i.e.
if (handle.kind==="directory")
handle.values() // only available on FileSystemDirectoryHandle
else
handle.move() // only available on FileSystemFileHandle
I think all of these also fall into https://github.com/microsoft/TypeScript-DOM-lib-generator/issues/1556#issuecomment-1536452712.
Can we at least consider improvements of current FileSystemHandle, I mean there is no obvious narrowing from FileSystemHandle to FileSystemDirectoryHandle (or FileSystemFileHandle)
Following would be nice to achieve:
const handle:FileSystemHandle;
if (handle.kind === "directory")
// handle to be narrowed to FileSystemDirectoryHandle so ie. handle.resolve() is available
else
// handle to be narrowed to FileSystemFileHandle so ie. handle.getFile() is available
Right now, an explicit casting is needed
if (handle.kind === "directory")
<FileSystemDirectoryHandle>handle...
That's #1494. Perhaps there can be type FileSystemAnyHandle = FileSystemDirectoryHandle | FileSystemFileHandle; and others APIs should use it instead of FileSystemHandle.