Git Product home page Git Product logo

Comments (1)

RyanCavanaugh avatar RyanCavanaugh commented on July 22, 2024

Reduced/renamed a bit

type SuccessStatus = 200;
type InvalidStatus = 400 | 404;
type Payload<T, S> = {
  data: T;
  status: S;
};
type ErrorMsg = { error: string };
type PayloadOrError<T> = Payload<T, SuccessStatus> | Payload<ErrorMsg, InvalidStatus>;

declare function responseHandler<T>(response: PayloadOrError<T>): T;

type PossibleTypesA =
  | Payload<{ id: string }, 200>
  | Payload<ErrorMsg, 400>;

type PossibleTypesB =
  | Payload<ErrorMsg, 400>
  | Payload<{ id: string }, 200>;

const dataA = responseHandler({} as PossibleTypesA);
const dataB = responseHandler({} as PossibleTypesB);

The problem here is that the "working" case is very dependent on inference successfully matching the Payload<{ id: string }, 200> part to the Payload<T, SuccessStatus> target rather than matching Payload<ErrorMsg, 400> to that target. The latter should be rejected because 400 isn't a valid SuccessStatus but the logic to do this is both fragile and expensive. This case does work when ErrorStatus is 400 instead of 400 | 404 so I think this could be fixed fairly straightforwardly by checking for conflicts in unions of literals instead of just literals. That said, generic inference is not in general capable of discarding all candidates that have conflicting outer properties (doing this for nonliterals isn't sound anyway, so don't do it).

from typescript.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.