Skip to content

Remove @ts-expect-error directive when getting the environment values #17

@cristobalgvera

Description

@cristobalgvera

Currently, I'm doing this to allow type-safe environments.

export class EnvironmentService<TEnvironment extends BaseEnvironment> {
  constructor(
    private readonly configService: ConfigService<TEnvironment, true>,
  ) {}

  get<Key extends keyof TEnvironment>(key: Key): TEnvironment[Key] {
    // FIX: Create the proper type for ConfigService.getOrThrow
    // @ts-expect-error: Type used by ConfigService is incorrect
    return this.configService.getOrThrow(key);
  }
}

I'm looking for a way to provide the right type to the getOrThrow function of the ConfigService.

If I remove the @ts-expect-error directive, the message thrown by TypeScript is the following:

typescript: Argument of type 'Key' is not assignable to parameter of type 'KeyOf<TEnvironment>'.
  Type 'keyof TEnvironment' is not assignable to type 'KeyOf<TEnvironment>'.
    Type 'string | number | symbol' is not assignable to type 'KeyOf<TEnvironment>'.
      Type 'string' is not assignable to type 'KeyOf<TEnvironment>'. [2345]

The KeyOf<T> type is an internal type of the ConfigService and has the following definition:

type KeyOf<T> = keyof T extends never ? string : keyof T;

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions