Skip to content
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

inferred generic incorrectly widened #43736

Open
DetachHead opened this issue Apr 19, 2021 · 2 comments
Open

inferred generic incorrectly widened #43736

DetachHead opened this issue Apr 19, 2021 · 2 comments
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@DetachHead
Copy link
Contributor

Bug Report

πŸ”Ž Search Terms

inferred generic widened

πŸ•— Version & Regression Information

4.3.0-beta

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

type Foo<T extends number> = T extends infer R? Bar<R>: never //compile error: Type 'R' does not satisfy the constraint 'number'

type Bar<T extends number> = never

πŸ™ Actual behavior

R was incorrectly widened to no longer be a number, even though it was inferred from T which is a number

πŸ™‚ Expected behavior

no error, as R is a number

@RyanCavanaugh RyanCavanaugh added In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Apr 19, 2021
@RyanCavanaugh
Copy link
Member

I think without loss of generality you could write Bar<T & R> instead here. I'm not sure if we could wire this up easily or not

@DetachHead
Copy link
Contributor Author

in that case this could potentially be related to #42938?

however many of my use cases for the infer keyword are simply using it to define sort of a "type variable" such that i don't have to repeat code. for example:

type TokenizeFormatDate<Format extends string> = '' extends Format
    ? []
    : LongestString<MatchStart<Format, Keys<FormatMap>>> extends infer Token
    ? Token extends string
        ? [Token, ...TokenizeFormatDate<TrimStart<Format, Length<Token>>>]
        : IndexOf<Format, Keys<FormatMap>> extends infer NextTokenIndex
        ? NextTokenIndex extends -1
            ? [Format]
            : [
                    TrimEnd<
                        Format,
                        // @ts-expect-error NextTokenIndex fails to narrow
                        NextTokenIndex
                    >,
                    ...TokenizeFormatDate<TrimStart<Format, IndexOf<Format, Keys<FormatMap>>>>
              ]
        : never
    : never

so in this case that solution would defeat the purpose, though i realise that's not really the intended use for them

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants