#Why can't I pass `Omit<MyUnion, 'one key'>` into a function that requires `MyUnion`?

12 messages · Page 1 of 1 (latest)

bitter sphinxBOT
#
bawdyinkslinger#0

Preview:ts export type WaitStrategy = ( debugNote?: string, timeoutMillis?: number ) => Promise<void> declare const waitForPassageEnd: WaitStrategy declare const waitForClickEnd: WaitStrategy export type WaitStrategyType = | ":passageend" | "click end" | WaitStrategy ...

trim sequoia
#

!ts

bitter sphinxBOT
#
export type WaitStrategy = (
  debugNote?: string,
  timeoutMillis?: number,
) => Promise<void>;
declare const waitForPassageEnd: WaitStrategy;
declare const waitForClickEnd: WaitStrategy;
export type WaitStrategyType = ":passageend" | "click end" | WaitStrategy;

export const buildWaitStrategy = (
  waitStrategyType: WaitStrategyType,
): WaitStrategy => {
  switch (waitStrategyType) {
    case ":passageend":
      return waitForPassageEnd;
    case "click end":
      return waitForClickEnd;
    default:
      return waitStrategyType;
  }
};

declare const parameter: Omit<WaitStrategyType, "click end">;
buildWaitStrategy(parameter);
//                ^^^^^^^^^
// Argument of type 'Omit<WaitStrategyType, "click end">' is not assignable to parameter of type 'WaitStrategyType'.
trim sequoia
#

I can't think of a reason why this shouldn't be allowed, but I think I'm blinded by my specific use case. Can someone explain why this is worth preventing?

#

I get the same error if I add a elements:

bitter sphinxBOT
#
bawdyinkslinger#0

Preview:ts export type WaitStrategy = ( debugNote?: string, timeoutMillis?: number ) => Promise<void> declare const waitForPassageEnd: WaitStrategy declare const waitForClickEnd: WaitStrategy export type WaitStrategyType = | ":passageend" | "click end" | WaitStrategy ...

trim sequoia
#

!ts

bitter sphinxBOT
#
export type WaitStrategy = (
  debugNote?: string,
  timeoutMillis?: number,
) => Promise<void>;
declare const waitForPassageEnd: WaitStrategy;
declare const waitForClickEnd: WaitStrategy;
export type WaitStrategyType = ":passageend" | "click end" | WaitStrategy;

export const buildWaitStrategy = (
  waitStrategyType: WaitStrategyType,
): WaitStrategy => {
  switch (waitStrategyType) {
    case ":passageend":
      return waitForPassageEnd;
    case "click end":
      return waitForClickEnd;
    default:
      return waitStrategyType;
  }
};

declare const parameter: ":passageend" | "click end" | WaitStrategy | "A new one";
buildWaitStrategy(parameter);
//                ^^^^^^^^^
// Argument of type 'WaitStrategy | ":passageend" | "click end" | "A new one"' is not assignable to parameter of type 'WaitStrategyType'.
//   Type '"A new one"' is not assignable to type 'WaitStrategyType'.
serene ravine
#

Omit is for objects, you want Exclude

trim sequoia
#

The term "Type" is to general