-
Notifications
You must be signed in to change notification settings - Fork 13.1k
Open
Labels
Effort: ModerateRequires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual".Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual".Help WantedYou can do thisYou can do thisSuggestionAn idea for TypeScriptAn idea for TypeScript
Milestone
Description
In the below code, using a typeof type guard and an equivalent (I thought) user-defined guard, only one error is produced.
var y: any;
// Built-in type guard
if (typeof y === "string") {
y.hello = true; // Correct error - 'hello' does not exist on type string
}
// Equivalent user-defined type guard
function f(x: any): x is string {
return typeof x === "string";
}
if (f(y)) {
y.hello = true; // No error with user-defined guard
}Playground demo. Looks like user-defined type guards won't narrow from any, in any circumstances, as far as I can tell.
Metadata
Metadata
Assignees
Labels
Effort: ModerateRequires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual".Requires experience with the TypeScript codebase, but feasible. Harder than "Effort: Casual".Help WantedYou can do thisYou can do thisSuggestionAn idea for TypeScriptAn idea for TypeScript