Skip to content

Type inference in Array<T>.filter() #46987

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

Closed
LouisBoucquet opened this issue Dec 2, 2021 Β· 4 comments
Closed

Type inference in Array<T>.filter() #46987

LouisBoucquet opened this issue Dec 2, 2021 Β· 4 comments
Labels
Duplicate An existing issue was already created

Comments

@LouisBoucquet
Copy link

Bug Report

πŸ”Ž Search Terms

Type inferance

πŸ•— Version & Regression Information

This bug occurs in version 4.5.2;

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

function tIsOfTypeP(t: T): t is P {
	// Logic
}

const array: T[] = [...];

// newArray is still of type T[]
const newArray = array
	.filter(t => tIsOfTypeP(t));

πŸ™ Actual behavior

The variable newArray is of type Array<T>.

πŸ™‚ Expected behavior

The variable newArray is of type Array<P>. You know that only elements will remain of type P.

@MartinJohns
Copy link
Contributor

Duplicate of #20218. You're not passing a type guard function to filter.

@LouisBoucquet
Copy link
Author

Shouldn't the type of the lambda become a typeguard?

@MartinJohns
Copy link
Contributor

MartinJohns commented Dec 2, 2021 β€’

No. See #10734.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Dec 2, 2021
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants