TypeScript newbie here, having problems with Object.fromEntries
. I'm trying to pares form and cast its values to something else. For example, given homogeneous shape of all data being strings, below works:
// example form content:
// {
// foo: 'bar',
// bar: 'biz'
// }
// return values
// {
// foo: 'bar',
// bar: 'biz'
// }
export const getFormData = (form: HTMLFormElement) => {
return Object.fromEntries<string>(
new FormData(form) as Iterable<[PropertyKey, string]>,
)
}
How I can cast values returned from the above to values others than strings? For example:
// example form content:
// {
// foo: '1',
// bar: 'biz',
// biz: 'false'
// }
// desired return values
// {
// foo: 1,
// bar: 'biz',
// biz: false
// }
export const getFormData = (form: HTMLFormElement) => {
return Object.fromEntries<string>(
new FormData(form) as Iterable<[PropertyKey, string]>,
)
}
My feeling is that I can somehow fix it with generics and interfaces but I'm at a loss how to proceed. If someone could ELI5 I would be grateful.
EDIT: my tsconfig.json
:
{
"compilerOptions": {
"moduleResolution": "node",
"module": "es2020",
"lib": ["esnext", "dom", "dom.iterable"],
"target": "es2020",
"downlevelIteration": true,
"importsNotUsedAsValues": "error",
"preserveValueImports": true,
"isolatedModules": true,
"resolveJsonModule": true,
"sourceMap": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"baseUrl": ".",
"allowJs": true,
"checkJs": true,
}
}
EDIT2: Better explanation of the desired outcome.