(no title)
9034725985 | 3 years ago
Lets say you have some input json that you want to slightly modify to something else. How would you do this with unknown? I can't just blindly replace any with unknown. I'd get errors like this: The right-hand side of a 'for...in' statement must be of type 'any', an object type or a type parameter, but here has type 'unknown'.ts(2407) For example, how can I do this better?
Remember the input json could be pretty much anything. I don't have a spec other than I only care about things that end with __c.
https://github.com/kusl/salesforcecontactmapper/blob/eff0b3e...
import { Output } from "./Output";
import { Preference } from "./Preference";
export function MyMap(input: unknown): Output {
const mypreferences = Array<Preference>();
for (const prefCode in input) {
if (prefCode.endsWith("__c")) {
if (prefCode === "IsInternalUpdate__c") {
continue;
}
let currentValue = "";
if (input[prefCode] !== null) {
currentValue = input[prefCode].toString();
}
if (currentValue === "true") {
currentValue = "True";
}
if (currentValue === "false") {
currentValue = "False";
}
const preference: Preference = {
PrefCode: prefCode,
CurrentValue: currentValue
}
mypreferences.push(preference);
}
}
const myOutput: Output = {
ContactId: input.Contact__c,
Email: input.ContactEmail__c,
IsInternalUpdate: true,
Preferences: mypreferences
}
return myOutput;
}
mxz3000|3 years ago
9034725985|3 years ago
You could say technically could be simply { "Unsubscribe__c": false } or even {} both of which are silly in my case because there is no key for me to identify who the person is but they are valid inputs.
Or the test case I have is https://github.com/kusl/salesforcecontactmapper/blob/eff0b3e...
Or the input could have a thousand key values and I only care about some of them. What should my object look like? How do I create a class that says everything that ends in "__c" is something I care about? I tried unknown. I tried Object. How do I fix this (and learn something so I fix all future code I write)?
manhinli|3 years ago
quickthrower2|3 years ago