TypeScript Troubles? We've Got Fixes! -- 2

Completed Posted 3 months ago Paid on delivery
Completed Paid on delivery

sendRequest function should be as follows:

2 arguments:

1st - data

2nd - callback

Can't be a single argument as an object, because of backward compatibility reasons.

Typings for sendRequest:

See the additional info section for detailed information.

Data must be one value in the interface allRequests. It cannot be more than one, or less than one. For example:

type req_getInfo = sendReqType<["getInfo"], ["info"]>;

// data must have getInfo key from [login to view URL], and nothing else

// returns info key from [login to view URL] in callback

type req_setEntityValue = sendReqType<

["setEntityValue", "entityId"],

["success"]

>;

// Similar to the getInfo request, but this time the data object must have both setEntityValue from [login to view URL] and entityId from [login to view URL]

let data = { getInfo: {}} // should be valid, callback should be info from [login to view URL]

data = { setEntityValue: {value: true}, entityId: 12345} // should be valid callback is success from [login to view URL]

data = {getInfo:{}, setEntityValue: {value: true}, entityId: 12345} // should be invalid, too many keys, doesn't match any known types

See playground link lines 149 - 167 on specific examples in context

Playground: [login to view URL]

So I have this function sendRequest(data, callback?)

Playground class on line 149

the value that we call in callback is based on the data argument. We know each possible value ahead of time, thanks to manual testing and the auto-generated [login to view URL] file.

What to know about sendRequest:

data is an object with what to request from the server

e.g: {getTime: {}} or {setEntityValue: {value:true}, entityId: 12345}

if any error occurred the server returns {response: {error: AppError}}

Non error response:

getTime: {response: {time: AppTime}}

setEntityValue: {response: {success: AppSuccess}}

I made a helper type to try to generate this: sendReqType<T,U>

T is an array of keys to data args e.g. ['setEntityValue','entityId']

U is an array of keys to values in callback arg e.g. ['success']

I made sendReqType for each possible key based on manual testing on returned types from server.

Then I made an allRequests interface:

interface allRequests {

getTime: req_getTime;

setEntityValue: req_setEntityValue;

// ...others

}

Current implementation:

sendRequest<T extends keyof allRequests>(

data: allRequests[T]["data"],

callback?: (message: {

response: allRequests[T]["response"];

}) => Promisable<void> | boolean

) {

// do stuff

}

This does work correctly if I pass in the generic. Example:

sendRequest<'getTime'>({getTIme:{}}, m=>[login to view URL]||[login to view URL])

But otherwise the callback arg seems to be all possible values, rather than just the one matching the key.

sendRequest({getTIme:{}, setEntityValue:{value:true}}, m=>[login to view URL]||[login to view URL]) // no error in arg1

// m is union of allRequests[keyof allRequests] basically

// error w/ callback: success does not exist on type {error: undefined, time: AppTime} ... and 9 others

I need some way of getting this to work without passing in a key for the generic every time.

JavaScript Software Architecture Typescript Javascript ES6 ES8 Javascript

Project ID: #37710942

About the project

6 proposals Remote project Active 3 mos ago

Awarded to:

Saqlainashraf1

Hi there, It seems like you're facing an issue where the TypeScript compiler is unable to infer the correct callback type based on the provided data. To address this, you can use conditional types and template literal More

$20 USD in 2 days
(1 Review)
1.4

6 freelancers are bidding on average $24 for this job

mobimubasir

Hello. I read your requirement i will do that. Please come on chat we will discuss more about this. I will waiting your reply.

$20 USD in 1 day
(23 Reviews)
4.7
ViruInsan

Hello Sir, I am happily serve you for the project, so I�m looking for opportunities at this time! My goal is to bring experience, maturity, focus, knowledge, and a healthy amount of hubris to my job in order to tackle More

$50 USD in 7 days
(10 Reviews)
3.8
mjanithbandara

Hello, I guess it's safe to say that I'm no stranger when it comes to handling complex development tasks. As an experienced full-stack developer proficient in JavaScript and a wide array of other languages including P More

$20 USD in 2 days
(1 Review)
2.5
lana54

Hi, Ciro I.. I have just read your project description in details and fully understood your requirements. I am ready to do your projects and I think I am the most proficient in your project. I am expert in languages su More

$15 USD in 6 days
(0 Reviews)
0.0
aqibbangash

As an experienced software engineer with extensive knowledge in Typescript and a keen eye for detail, I thrive on finding elegant solutions to complex problems like the one you've presented. This project requires someo More

$20 USD in 1 day
(1 Review)
0.0