Re:A 'CSS reset' for TypeScript, improving types for common ...
by erur on Friday March 03, 2023 @05:56AM
(#35008036)
You can go incredibly far with the type system.
If you wanna go down that rabbit hole, I'd suggest Typescript type challenges. Completely blew my mind when I came across it the first time.
When you start out with typescript you might think Omit<> and Partial<> are cool but holy hell, you can do so much more.
Re:A 'CSS reset' for TypeScript, improving types for common ...
by wizofaus on Friday March 03, 2023 @05:52AM
(#35008012)
What's wrong with Object?
Re:A 'CSS reset' for TypeScript, improving types for common ...
by 9034725985 on Friday March 03, 2023 @06:25AM
(#35008190)
If I set input as Object,
if (input[prefCode] !== null) {
currentValue = input[prefCode].toString();
}
in the lines above, I see a red underline under input[prefCode]> Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Object'. No index signature with a parameter of type 'string' was found on type 'Object'.ts(7053)
Re:A 'CSS reset' for TypeScript, improving types for common ...
by wizofaus on Friday March 03, 2023 @01:59PM
(#35013614)
Could you use a helper like
function getProperty<T, K extends keyof T>(o: T, propertyName: K): T[K] {
return o[propertyName];
}
?
Re:A 'CSS reset' for TypeScript, improving types for common ...
by sarahdellysse on Friday March 03, 2023 @12:01PM
(#35012179)
input: Record<string, unknown>
Re:A 'CSS reset' for TypeScript, improving types for common ...
by 9034725985 on Friday March 03, 2023 @02:27PM
(#35013917)
Thank you. That gives me a red underline under ContactId and Email now. I think because input.Contact__c and input.ContactEmail__c are unknown. I think this is the right direction. What is my next step?
const myOutput: Output = {
ContactId: input.Contact__c,
Email: input.ContactEmail__c,
IsInternalUpdate: true,
Preferences: mypreferences
}
(property) Output.ContactId: string Type 'unknown' is not assignable to type 'string'.ts(2322) Output.ts(4, 5): The expected type comes from property 'ContactId' which is declared here on type 'Output'
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)?