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

Type of pointer to object is treated differently from type of object #59104

Closed
nvlang opened this issue Jul 2, 2024 · 3 comments
Closed

Type of pointer to object is treated differently from type of object #59104

nvlang opened this issue Jul 2, 2024 · 3 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@nvlang
Copy link

nvlang commented Jul 2, 2024

πŸ”Ž Search Terms

inconsistent inference of type of pointer / alias in generic function

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about bugs

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.6.0-dev.20240701#code/KYDwDg9gTgLgBAMwK4DsDGMCWEV1AQwFswAbYAHgDUA+ACgCg4nFUNsUB5KAQSinwCeALkbMxAHzi1aASjgBeanFQATYAkwpgKmaLFNJtSnhAxgKFQGc4lmFE0BzOAH44lANoBdOEOUX1mtoyADT0cgDeenCYCFK8-AIAdJiW8YK0yOhYOFxpAjIRcFFiaDi2cJCaZlAKLFnsuXyCANxF+sxk8ABu+CS+tDACYMAQsZlsOTxN+e4oSIQARsBQnq0A9GtwEADWgsUdwPAOSPhQKgCM-V2+qNsoEADuKHKKcF3R1gNDI2Os2ZxTBIyWbzJYrdabZZQaD7JidODHU4qABMVxuKDuj2eCiU7xSUkGw1GFQgVWWwLmi2WqzgGy2uwEUQAvkx6EygA

πŸ’» Code

export function example<V>(
    functionOrArray:
        | (() => undefined)
        | (V extends string ? V[] : undefined),
) {
    if (Array.isArray(functionOrArray)) { 
        const pointer = functionOrArray; 
        let val: (typeof functionOrArray)[number]; // okay
        let guard1: (v: unknown) => v is (typeof functionOrArray)[number]; // error
        let guard2: (v: unknown) => v is (typeof pointer)[number]; // okay
    }  
}

πŸ™ Actual behavior

(v: unknown) => v is (typeof alias)[number] works, but (v: unknown) => v is (typeof functionOrArray)[number] doesn't.

πŸ™‚ Expected behavior

Expected alias and functionOrArray to either both work or both not work.

Additional information about the issue

No response

@MartinJohns
Copy link
Contributor

Duplicate of #9998.

@RyanCavanaugh
Copy link
Member

typeof in that position is going to use the non-control-flowed (i.e. declared, not narrowed) type of the variable, and pointer and functionOrArray have different declared types

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jul 9, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants