Great question! I've run into this before. The key thing to keep in mind is to follow the official documentation closely and test your implementation with edge cases. Happy to elaborate if you need more specifics.
asked 3 months ago
1
8.3K
I have a utility function that returns different types depending on the input parameter:
function transform(input: string | number) {
if (typeof input === 'string') return input.toUpperCase();
return input * 2;
}
TypeScript infers the return type as string | number, but I want it to be narrowed based on the input. Is this possible with overloads or conditional types?
Great question! I've run into this before. The key thing to keep in mind is to follow the official documentation closely and test your implementation with edge cases. Happy to elaborate if you need more specifics.
1
53
1