#Return T or undefined if second param is not present

24 messages · Page 1 of 1 (latest)

tight tundra
#

Given the following function:

getOption<T = any> (name: string, defaultValue?: T): T

How can I return T | undefined if defaultValue is not used?

const userId = this.getOption<number>('user-id', 1)
// ^? number

const accountId = this.getOption<number>('account-id')
// ^? number | undefined
echo flume
#

though that second signature seems kinda fishy. how will you ensure at runtime that it's a number?

tight tundra
#

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

echo flume
tight tundra
#

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?

echo flume
#

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?

tight tundra
#
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

echo flume
#

ah you're reading env variables. i see

#

isn't it always going to be string?

tight tundra
#

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

echo flume
#

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

tight tundra
#

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

echo flume
#

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)

tight tundra
#

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