type-fest icon indicating copy to clipboard operation
type-fest copied to clipboard

SetRequired unable to infer type

Open xenoterracide opened this issue 4 years ago • 5 comments

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

  • We're using Polar.sh so you can upvote and help fund this issue.
  • The funding will be given to active contributors.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar

xenoterracide avatar Nov 10 '20 21:11 xenoterracide

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


// @crhistianramirez

sindresorhus avatar Nov 10 '20 23:11 sindresorhus

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

xenoterracide avatar Nov 11 '20 01:11 xenoterracide

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

I did it for you: TS Playground

papb avatar Nov 11 '20 13:11 papb

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.

papb avatar Nov 11 '20 14:11 papb

@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.

xenoterracide avatar Nov 12 '20 02:11 xenoterracide