sindresorhus / type-fest

A collection of essential TypeScript types
Creative Commons Zero v1.0 Universal
14k stars 531 forks source link

SetRequired unable to infer type #140

Open xenoterracide opened 3 years ago

xenoterracide commented 3 years ago
export interface ModelEntity<ID extends string | number | unknown> extends Identifiable<ID> {
  readonly createdAt: ZonedDateTime;
  modifiedAt: ZonedDateTime;
}

export interface AggregateRoot<ID extends string | number | unknown, STATUS>
  extends ModelEntity<ID> {
  readonly status: STATUS;
}

export type AggregateOptsExcept<T extends AggregateRoot<unknown, unknown>> = Except<
  T,
  'id' | 'createdAt' | 'modifiedAt'
>;

type PartialAggregateOpts<T extends AggregateRoot<unknown, unknown>> = Partial<
  AggregateOptsExcept<T>
>;

export type AggregateOpts<T extends AggregateRoot<unknown, unknown>> = SetRequired<
  PartialAggregateOpts<T>, 'status' // compile error here, it doesn't recognize status
>;
app-lib/graph/packages/app/src/shared/model.ts:59:3 - error TS2344: Type 'string' does not satisfy the constraint 'Exclude<keyof T, "id" | "createdAt" | "modifiedAt">'.

59   'status'
     ~~~~~~~~

Upvote & Fund

Fund with Polar

sindresorhus commented 3 years ago

What TypeScript version are you using? Did you try the latest one?


// @crhistianramirez

xenoterracide commented 3 years ago

looks like not quite the latest

> yarn info typescript                                               # services -> feature/RS2-1286 $ — ! ? RC=141
└─ typescript@patch:typescript@npm%3A4.0.2#builtin<compat/typescript>::version=4.0.2&hash=5bf698
   ├─ Version: 4.0.2
   │
   └─ Exported Binaries
      ├─ tsc
      └─ tsserver
papb commented 3 years ago

@xenoterracide Hello! Please make an effort to create a minimal reproducible example in the future.

I did it for you: TS Playground

papb commented 3 years ago

By the way, interestingly, the compiler error message changes (for worse, IMO) from TS 3.9 to TS 4.0. You can quickly see this by changing the version in the playground.

xenoterracide commented 3 years ago

@xenoterracide Hello! Please make an effort to create a minimal reproducible example in the future.

sorry about that, I'd hoped my pasted code would be enough.