Typing transformations in TypeScript

type ApiReponse = {
foo: number;
bar: string;
veryBigNumber: string; // overflows JS Number type, so it's encoded as a string by the API endpoint
date: string; // as ISOString "2020-05-30T11:39:40.230Z"
}
type TransformedApiReponse = ?

const request = async ():Promise<TransformedApiReponse[]> => {
const result = await fetch<ApiReponse[]>(url)
return result.map(response => ({
...response,
veryBigNumber: new BigNumber(response.veryBigNumber),
date: new Date(response.date),
}))
}
// before
type TransformedApiReponse = {
foo: number;
bar: string;
veryBigNumber: BigNumber; // changed
date: Date; // changed
}

// now
type TransformedApiReponse = Omit<ApiReponse, "date" | "veryBigNumber"> & {
veryBigNumber: BigNumber;
date: Date;
}

type New = Omit<ApiReponse,"date" | "veryBigNumber">
& {
veryBigNumber: BigNumber;
date: Date;
}
Omit, Pick, Partial, ReturnType

--

--

--

Full Stack Software Engineer #javascript #EOS. Into Recreational Math / CS 🤯 Just message me about anything, my mind is open.

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to make Isotope work in a Gatsby site

React Native And Redux — Part 3

Learn Nest.js Middleware

Bootcamp Week 6: Halftime

Magical Javascript

Full Stack Web Application using React, Node.js, Express and Webpack

“Error: Loading Chunk XX Failed” with Angular Lazy Loaded Modules

useCallback vs useMemo

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Christoph Michel

Christoph Michel

Full Stack Software Engineer #javascript #EOS. Into Recreational Math / CS 🤯 Just message me about anything, my mind is open.

More from Medium

Useful Tools for Defi Kingdoms (Ahead of Crystalvale Launch)

Redundant server setup

How you can use Alluo to generate 16% on your USD stablecoins

Golang Gin Server export Typescript REST-API