basickarl
basickarl

Reputation: 40551

Argument of type 'typeof Environment' is not assignable to parameter of type 'Environment'

Code:

enum Environment {
    Production = 'production',
    Development = 'development',
    Test = 'test'
}

export class Config {
    public constructor(EnvProd: Environment = Environment.Production, EnvEnum = Environment) {
        Config.createConsoleLog(EnvProd, EnvEnum, console);
    }

    private static createConsoleLog(EnvProd: Environment, EnvEnum: Environment, console: Console): void {
        console.log(EnvProd, EnvEnum);
    }
}

Gets the error:

karlm@karl-Dell-Precision-M3800:~/dev/sg$ npx ts-node application/libs/config/index.ts 
⨯ Unable to compile TypeScript:
application/libs/config/index.ts:9:42 - error TS2345: Argument of type 'typeof Environment' is not assignable to parameter of type 'Environment'.

9         Config.createConsoleLog(EnvProd, EnvEnum, console);
                                           ~~~~~~~

Been trying to figure out why I can't get this to work.

Upvotes: 1

Views: 1474

Answers (1)

Aleksey L.
Aleksey L.

Reputation: 37996

Currently you're sending the whole enum object where enum value expected, hence the error.

If you want to allow passing the "enum object" the signature should be:

private static createConsoleLog(EnvProd: Environment, EnvEnum: typeof Environment, console: Console): void

Pay attention to typeof Environment (enum object is also value)

Upvotes: 1

Related Questions