unions-intersections.ts 554 B

12345678910111213141516171819202122232425262728
  1. // 一般 union → anyOf(含 null 的对象 union),交叉类型展平为一个 object。
  2. interface Success {
  3. ok: true;
  4. value: string;
  5. }
  6. interface Failure {
  7. ok: false;
  8. error: string;
  9. }
  10. type Outcome = Success | Failure;
  11. interface HasId {
  12. id: string;
  13. }
  14. interface HasTimestamp {
  15. createdAtEpoch: number;
  16. }
  17. type Record_ = HasId & HasTimestamp;
  18. interface Envelope {
  19. outcome: Outcome;
  20. maybe: Success | null;
  21. record: Record_;
  22. }
  23. const g = agent("g");
  24. const envelope = await g.ask<Envelope>("wrap it up");
  25. log(JSON.stringify(envelope));