Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
cristobalgvera opened this issue May 13, 2023 · 0 comments
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@cristobalgvera
Copy link
Owner

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;
@cristobalgvera cristobalgvera added enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers labels May 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

1 participant