#Using return type of inner function

23 messages · Page 1 of 1 (latest)

dim dagger
#

how can i use infered type as the return type of validateEnv function here?

export const validateEnv = () => {
    return cleanEnv(process.env, {
        DISCORD_BOT_TOKEN: str(),
        DISCORD_BOT_ACCESS_KEY: minLengthString(32)(),
        // NODE_ENV: str({ choices: ['development', 'test', 'production', 'staging'] }),
    });
}
haughty anvil
#

It will be inferred by default

#

If you need it to be explicit you can do something like ReturnType<typeof cleanEnv>

#

adding generic parameters to cleanEnv if necessary, I suppose

dim dagger
#

yes typescript's by default infered type works very good but i am getting this eslint error:
Missing return type on function.eslint@typescript-eslint/explicit-function-return-type
thats why thinking there might be better way

dim dagger
#

but it desn't give type suggestion

#

now getting any

gray heronBOT
#

function foo<T>(v: T): T {
  return v
}
type R = ReturnType<typeof foo<string>>
//   ^? - type R = string
haughty anvil
#

Do you need to specify the generic parameter like here?

#

You can inspect the types like the code above

dim dagger
#

yes i guess,
function cleanEnv<S>(environment: unknown, specs: S, options?: CleanOptions<S>): CleanedEnv<S>

haughty anvil
#

OK yeah

dim dagger
#

so everytime i make change in env file i have to adjust the types, i better just shut the eslint rule for this one and use default infered type

#

xD

haughty anvil
#

I like explicit return types, but I don't have a rule to require it everytime.

#

So yeah disabling it wouldn't seem wrong to me.

#

You could store your env in an external object

#

and use : CleanedEnv<typeof envObject>

molten urchin
#

I personally don't think explicit-function-return-type is very useful.

#

If you are writing a library, you can look at turning on explicit-module-boundary-types instead.

haughty anvil
#
const envSchema = {
  DISCORD_BOT_TOKEN: str(),
  DISCORD_BOT_ACCESS_KEY: minLengthString(32)()
}

export const validateEnv = (): ReturnType<typeof cleanEnv<typeof envSchema>> {
  return cleanEnv(process.env, envSchema))
}
dim dagger
#

well i am not entirely disableing it but only for this function tho.