You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
typeMerge<T,U>=Textendsany[] ? [...T,U] : [T,U];exportinterfaceIParser<T>{prop: T;then<Y>(p: IParser<Y>): IParser<Merge<T,Y>>;// Comment or remove this line to change the inferred type of `const Trigger`or<Y>(p: IParser<Y>): IParser<T|Y>;}interfaceParser<T>extendsIParser<T>{prop: T;}////////////////////////////////////////////////////////////////////////////////////////////////////////////////declareconstreturns: <T>(v: T)=>Parser<T>;declareconstLeftParser: IParser<string|null>;constTrigger=LeftParser.or(returns(null));// Should be IParser<string | null>
🙁 Actual behavior
The type of const Trigger gets inferred as IParser<string | [null] | null>.
🙂 Expected behavior
The type of const Trigger gets inferred as IParser<string | null> or IParser<unknown>, the [null] should not be part of the type signature.
Additional information about the issue
I’ve been working on custom typings for Masala Parser in version 0.6 and hit this problem.
Changing line 17 to declare const returns: <T>(v: T) => IParser<T>; fixes the problem as well.
So when we're doing structural inference, there's a match on the type of then wherein a valid candidate inference site for T is IParser<Merge<T, Y>> <-> IParser<[null, Y]>, and [null, Y] is inferred into Merge<T, U>, which is a match for extends any[] and it appears that [null] is the thing you can substitute for T to get [null, Y] as an output.
Thank you very much! I had a feeling that it would be something like that, but couldn’t quite put my finger on it.
Changing the Merge type to type Merge<T, U> = T extends [...rest: infer R] ? [...R, U] : [T, U] seems to fix the inferred types for all cases.
🔎 Search Terms
🕗 Version & Regression Information
interface Foo<T> { }
⏯ Playground Link
https://www.typescriptlang.org/play/?#code/C4TwDgpgBAshBOBzCAeAKgGigVQHxQF4o0oIAPYCAOwBMBnKAQypAG0BdKAfilYDoBmHJwBcvIdnYBuAFAzyYAPbxgUAJZVK8AGaMAxtACSABUbw6CdPgDeMqFDDxFYMWll2owABbUUATVwAChcoEzMLeH9cAEoxMPNLOCRUIQDcKSgAekyoAGFFAFsC6lVlKHgIAsUAN2hvNQYAGw06xSg9L2ZkTx91Km0ECppPcGhFbSgAAz1FKjpVNHg1RGR4Sbl7ZSjguNMEyIDY0L2I9CgAHygA2QBfOQ0tXQMoE8s0fHJKWgZ40-eoWz2RzOVy3OTZCGQqHQmGwuHwhGIpHI2FyGgQPSNMzQGZzVQVYAAV3gczEVkC1Vc0UI+FekXe7nRmOx7Vm8ygABkINpgHTduFLPMllREBcoFRCY1Guk5Lj2YtlqtCJzubyBfA+MpAgTiXNAhKpdFohlslAAMpeRSS4YAIyMdJQQo0osuBulMiAA
💻 Code
🙁 Actual behavior
The type of
const Trigger
gets inferred asIParser<string | [null] | null>
.🙂 Expected behavior
The type of
const Trigger
gets inferred asIParser<string | null>
orIParser<unknown>
, the[null]
should not be part of the type signature.Additional information about the issue
I’ve been working on custom typings for Masala Parser in version 0.6 and hit this problem.
Changing line 17 to
declare const returns: <T>(v: T) => IParser<T>;
fixes the problem as well.The complete code can be found here: https://gist.github.com/max-m/7678f14661bfe3f6028593c96b66c0b5
The official documentation of Masala Parser’s
then
function can be found here.The text was updated successfully, but these errors were encountered: