#Return T or undefined if second param is not present
24 messages · Page 1 of 1 (latest)
you could use overload signatures: https://www.typescriptlang.org/docs/handbook/2/functions.html#function-overloads
though that second signature seems kinda fishy. how will you ensure at runtime that it's a number?
Good catch, I'm actually just handling the type, I'm assuming that who is gonna use this, make sure the type he's providing on the function type param is the right one.
About the solution, yeah, seems what I'm looking for... I was actually trying to handle it with a more fancy complicated type expression
i know some libraries do this (i'm looking at you, axios), but personally i'd rather see an as at the call site if the caller is going to assume that the value is of a certain type without doing any validation. IMO it's better to be explicit about stuff like that rather than hiding it in a type parameter
So, what is the best practice in this case? Should the function return any and then cast the function return with the as whatever the function is used?
i'd probably need more context to give specific advice, but unknown is safer than any. is it really unknown though? the function doesn't have any constraints whatsoever on what it returns?
protected getOption<T = any> (name: string): T | undefined
protected getOption<T = any> (name: string, defaultValue?: T): T {
const value = process.env[`npm_config_${name}`]
if (value === undefined) {
return defaultValue as T
}
if (value === 'true') {
return true as T
}
return value as T
}
this is the complete function
it just takes params from npm command line and uses the default value if its set
If the env doest exists it returns undefined
The envs are not being loaded from a .env file
I'm actually getting params out of a npm command. Ex: npm run some-command --option=1
right, sorry it can be undefined | string, but never like undefined | number or whatever, right? unless i'm mistaken i don't think process.env gets any magic processing and the values are always strings
that is, if you run npm run some-command --option=1 and then console.log(typeof process.env.npm_config_option) you'll get "string", right? the value will be "1", not 1, unless i am terribly mistaken
you right
I'm getting into your advice and going to use lodash to check what is the kind of value of the env variable and parsing it accordinaly
yeah, i guess you need to do your own parsing. you might want to consider having some way to configure what types are expected rather than trying to infer it from the value. that way if there's usage like npm run some-command --password=1234 you won't accidentally infer the password as a number rather than a numeric string. that's what most CLI option libraries i've used do (e.g. https://www.npmjs.com/package/ts-command-line-args, but there are zillions of these kinds of libraries)
thanks for the suggestion, but i would rather use a superficial implementation of my own 10 lines code than use a library that has a ton of features that i dont need
also i'm learning with the challenge of doing it myself
thank you again @echo flume the overloads did the trick and your advices was really appreciated