-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
the types 'T' and (anything else) have no overlap #32768
Comments
My use-case approximates to the following: const isNum = (v: unknown) => {
if (typeof v !== 'number') throw new Error('Not a number');
return v;
};
const isString = (v: unknown) => {
if (typeof v !== 'string') throw new Error('Not a string');
return v;
};
function makeExampleValue<T>(validator: (v: unknown) => T): T {
if (parser === isNum) return 42;
if (parser === isString) return 'something';
throw new Error('unknown type');
} I'm considering restructuring to avoid the need for what is effectively a switch in a monolith function, but the reported error is incorrect in any case. |
|
@AnyhowStep that works, so I guess I'm confused why |
If it can't infer As for why you need to explicitly add ¯\(ツ)/¯ |
@DanielRosenwasser what's the reasoning behind the current comparability rules with type parameters? |
Without an explicit constraint I think the default constraint used is the empty object type (if you do const constraint = getConstraintOfType(<TypeVariable>source);
if (!constraint || (source.flags & TypeFlags.TypeParameter && constraint.flags & TypeFlags.Any)) {
// A type variable with no constraint is not related to the non-primitive object type.
if (result = isRelatedTo(emptyObjectType, extractTypesOfKind(target, ~TypeFlags.NonPrimitive))) {
errorInfo = saveErrorInfo;
return result;
}
} I'm wondering if this was just a missed case in #30637? |
@RyanCavanaugh We have another issue open about this, but if you tried to "fix" it, you might end up with weird behavior where two unrelated type parameters |
|
It's actually the other way around -
I would consider that a bug, and I've opened #32814 to track it. I would guess it's likely either a bug in our |
We're going to track this at #32814 for clarity |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
FYI, the default base constraint is |
Yeah I knew the design had changed, but I wasn't sure if a case had been missed. That code looked related, though tbh, I'm not really sure what's going on. |
TypeScript Version: 3.5.3
Search Terms:
Code
Expected behavior:
No error;
check
returnstrue
if givennum
(or an alias of it), andfalse
otherwise.Actual behavior:
I observed this in a slightly more complex case but with the same idea:
Playground Link:
https://www.typescriptlang.org/play/#code/MYewdgzgLgBGCuBbGBeGBGA3AKAGbzGCgEtwZgALAU2AGsAeAFQD4AKADwC4ZGBKGAN7YYMAE5Uo8UWBjtUKNAkQ4Avtko1arJb0xA
The text was updated successfully, but these errors were encountered: