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 narrowing for predicates works with implicit if, fails with explicit if #53005

Closed
ljharb opened this issue Feb 27, 2023 · 2 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@ljharb
Copy link
Contributor

ljharb commented Feb 27, 2023

Bug Report

See https://tsplay.dev/Nr8YVW - it seems like the type narrowing allows truthy/falsy checks, but doesn't support when the predicate's return value is explicitly compared to true or false.

🔎 Search Terms

i guess type narrowing, predicates?

🕗 Version & Regression Information

This occurs in the latest nightly on the TS playground; see above. I assume it's always been the case since predicate support was added, but haven't checked.

⏯ Playground Link

https://tsplay.dev/Nr8YVW

💻 Code

function isFunction(x: unknown): x is Function {
	return typeof x === 'function';
}

const x: unknown = function () {};

if (isFunction(x)) {
	x;
	// x is a function here
}

if (!isFunction(x)) {
	x;
	// x is not a function here (should be more specific than `unknown`, but ¯\_(ツ)_/¯)
}

if (isFunction(x) !== false) {
	x;
	// x should be a function here
}

if (isFunction(x) === true) {
	x;
	// x should be a function here
}

🙁 Actual behavior

See above.

🙂 Expected behavior

See above.

@fatcerberus
Copy link

Duplicate of #44366

@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