Skip to content

Array.isArray no longer narrows 'object' values in 5.1 #54917

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
AustinGrey opened this issue Jul 7, 2023 · 3 comments
Closed

Array.isArray no longer narrows 'object' values in 5.1 #54917

AustinGrey opened this issue Jul 7, 2023 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@AustinGrey
Copy link

AustinGrey commented Jul 7, 2023

Bug Report

πŸ”Ž Search Terms

All open issues referencing "Array.isArray"
"typeguarding arrays from object values"

πŸ•— Version & Regression Information

  • This changed between versions 5 and 5.1

⏯ Playground Link

Playground link with relevant code

πŸ’» Code

export function searchObject<T extends object>(
	obj: T,
	properties: (keyof T)[],
) {
	for (const property of properties) {
		const val = obj[property];
		if (Array.isArray(val)) {
			val.forEach(() => {});
		}
	}
}

πŸ™ Actual behavior

val may be an array, but Typescript reports that forEach is not defined on val because T[keyof T] cannot be an array.

πŸ™‚ Expected behavior

val is 'narrowed' to T[keyof T] & any[]

@typescript-bot
Copy link
Collaborator

The change between origin/release-5.0 and origin/release-5.1 occurred at 84a09c7.

@RyanCavanaugh
Copy link
Member

The originating change is being reverted. Track at #54845

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jul 7, 2023
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" 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 10, 2023
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

3 participants