Comments (1)
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)
- spread operator infer to wrong type with method override and generic HOT 6
- Intellisense is slow and some things don't work HOT 4
- Constructor cannot access own protected instance when typed as union HOT 2
- This comparison appears to be unintentional because the types 'U[K]' and 'T[K]' have no overlap.ts(2367) HOT 3
- [isolatedDeclarations] Optional parameter in arrow function typed as imported generic type gets emitted as `any`
- Easier use of indexed access types on nullable/optional types HOT 2
- Narrow conditional type HOT 1
- Error for path not found when ended with asterisk
- `tryGetPerformanceHooks` throws on Stackblitz with ts 5.5.3 HOT 3
- Avoid providing promise methods as completions at return positions in non-async functions
- Consider disallowing `in` operator use with arbitrary key operands on closed types for which the keys are known HOT 2
- Convert to ESM uses template strings instead of string literals HOT 1
- Running type checker produces js out of memory error HOT 2
- Callback argument type not inferred for union of interfaces HOT 1
- Como Hackear Instagram 2024 Hackear Instagram Metodos Atualizados #221857
- [transpileDeclaration API] ts.transpileDeclaration always returns empty sourceMapText
- Incorrect Error Message: "Cannot assign to 'name' because it is a constant" Instead of "Identifier 'name' is not defined" for Undefined Identifier Assignment HOT 2
- Generic with extends yields to different type than directly specifying it HOT 4
- Instantiation expressions don't reject incompatible signatures in situations with mixed call and construct signatures HOT 1
- Parameters of overridden methods are made implicit any when using JSDoc `@override` HOT 13
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typescript.