[TypeScript] Omit `undefined` from return type if `defaultValue` is specified
acro5piano opened this issue · 3 comments
Hi! First of all, thank you for the amazing library.
I found env.get('env', 'default')
returns string | undefined
which is not possible, because it always returns string
.
This can be fixed by using Overload signature like this:
// dist/env.d.ts
export class Env {
get(key: string): string | undefined;
get(key: string, defaultValue: string): string;
}
I took a look at the code and possible solutions are the following.
- Re-write code in TypeScript
- Create the TypeScript declaration file manually
Can I send a PR for this? If so, which solution do you prefer?
Best regards,
Thanks, I'm not interested in maintaining a separate TypeScript definition file or converting the project to TypeScript. If there's a way to do it just with JavaScript and JSDoc, then I'm happy to consider it.
Thank you for your reply.
If there's a way to do it just with JavaScript and JSDoc, then I'm happy to consider it.
I searched about JSDoc, but I don't think there is the way to express overload signature.
I'm not interested in maintaining a separate TypeScript definition file or converting the project to TypeScript
Then, the only way is to create a separate method for this, like
const username = env.ensure('USERNAME', 'defaultValue')
// `username` is always string
Or modify the generated declaration file by running some script, but this is a too ugly solution.
Yeah, I didn’t think there was a way to do it, but thanks for investigating.