eightyfive
eightyfive

Reputation: 4721

Why is Promise<any> a valid TS return type?

Why does Promise<any> seem to have a "Wozniak free pass for life" in that situation?

Shouldn't it comply to the return type of Layer (Promise<Response>)?

What is the correct way to enforce Promise<Response> as the return type?

type Layer = (req: Request) => Promise<Response>;

type Middleware = (next: Layer) => Layer;

export const json: Middleware = (next: Layer) => async (req: Request) => {
  const res = await next(req);

  if (res.headers.get('Content-Type') === 'application/json') {
    const data = await res.json();

    return data; // -> Type "any" - No TS error, why ??
  }

  return res; // -> Type "Response" - All good
};

Upvotes: 0

Views: 57

Answers (1)

Ricky Mo
Ricky Mo

Reputation: 7648

any is a way to "opt-out of type checking", essentially means "Treat it as regular javascript. I don't care type-checking for this thing". So any is assignable to anything (except never as @equt pointed out) and anything is assignable to any.

Upvotes: 1

Related Questions