As seen in the GitHub link, the definition of Blob
is specified:
/** A file-like object of immutable, raw data. Blobs represent data that isn't necessarily in a JavaScript-native format. The File interface is based on Blob, inheriting blob functionality and expanding it to support files on the user's system. */
interface Blob {
readonly size: number;
readonly type: string;
slice(start?: number, end?: number, contentType?: string): Blob;
}
The question arises about constraining this type to ensure a specific type
for the Blob.
It seems that utilizing a Mapped Type or an intersection type could be the solution. However, as someone new to TypeScript, clarity is sought on whether Mapped Types modify all properties and how to properly enforce constraints.
An attempt at achieving this constraint is demonstrated below:
type HTMLBlob = {
[P in keyof Blob]?: Blob[P];
} & { type: 'text/html' }
function handleHTMLBlob(blob : HTMLBlob) {
// ...
}
const blob : HTMLBlob = new Blob(['<b>Test</b>'], { type: 'text/html' });
handleHTMLBlob(blob);
Nevertheless, an error is encountered where 'Blob' is not assignable to type 'HTMLBlob' due to a mismatch in the 'type' property.
The challenge then lies in finding a way to constrain a property to a specific subtype without the use of generics, while also ensuring automatic inheritance of any potential future properties added to the base Blob
type.