I am having certain types that are categorized based on mimetypes
export type MimeType = 'image' | 'application' | 'text';
export type ApplicationMimeType = '.pdf' | '.zip';
export type ImageMimeType = '.gif' | '.png' | '.jpeg' | '.jpg' | '.svg' ;
export type TextType = '.csv' | '.txt';
export type ExtensionTypes = ImageMimeType[] | ApplicationMimeType[] | TextType[];
export type FileType = {
image?: ImageMimeType[];
application? : ApplicationMimeType[];
text? : TextType[]
};
Now when I use it in a function, then use Object.entries when a certain object is passed, the key type is set to string. Is there a way for this key to be set to the type of MimeType?
I tried the solution that is given in this link: Typescript Key-Value relation preserving Object.entries type
Code I tried that is giving me error Type 'undefined' is not assignable to type 'string[]'
.
How can I restrict key to be MimeType
and extensions to be ExtensionTypes
export type Entries<T> = {
[K in keyof T]: [extensions: T[K]][];
}[keyof T][];
export const getAcceptedFileTypes = (mimeTypes: FileType) => {
const acceptedTypes = {};
Object.entries(mimeTypes as Entries<mimeTypes>).forEach(([key, extensions]) => {
if (key === 'text') {
acceptedTypes['text/*'] = extensions;
} else if (key === 'image') {
extensions.forEach(
(image) => (acceptedTypes[`image/${image.substring(1)}`] = [image])
);
} else {
extensions.forEach(
(application) =>
(acceptedTypes[`application/${application.substring(1)}`] = [
application,
])
);
}
});
return acceptedTypes;
};
getAcceptedFileTypes({ image: ['.png'], application: [] })