-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Custom type guard function #3262
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
9515947
Adds custom type guard
tinganho 284b9db
Fixes old tests
tinganho f8e2b99
Adds tests
tinganho b7d1df6
Adds type guard methods
tinganho b054234
Addresses PR feedback
tinganho 3aa0839
Fixes rebase issues
tinganho 19e7256
Adds PR feedback and removed references to typeguard (class) methods
tinganho e1f82c5
Removes old type predicate functions
tinganho f8dc6bb
Changed name from method to buildDisplay
tinganho 76bc9be
Refactor to-string functions
tinganho 250bd0d
Change name from data to info
tinganho 48acb48
Add back void annotation
tinganho a73bf31
Fixes CR feedback
tinganho fa9a914
Adds error for non-return positioned type predicates and changed pars…
tinganho 487dff5
Fixes CR feedback
tinganho b1a8a5f
Addresses CR feedback
tinganho 51a43dd
Addresses CR feedback
tinganho ebe755b
Addresses CR feedback
tinganho efb7013
Merge commits from master
tinganho File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
//// [typeGuardFunction.ts] | ||
|
||
class A { | ||
propA: number; | ||
} | ||
|
||
class B { | ||
propB: number; | ||
} | ||
|
||
class C extends A { | ||
propC: number; | ||
} | ||
|
||
declare function isA(p1: any): p1 is A; | ||
declare function isB(p1: any): p1 is B; | ||
declare function isC(p1: any): p1 is C; | ||
|
||
declare function retC(): C; | ||
|
||
var a: A; | ||
var b: B; | ||
|
||
// Basic | ||
if (isC(a)) { | ||
a.propC; | ||
} | ||
|
||
// Sub type | ||
var subType: C; | ||
if(isA(subType)) { | ||
subType.propC; | ||
} | ||
|
||
// Union type | ||
var union: A | B; | ||
if(isA(union)) { | ||
union.propA; | ||
} | ||
|
||
// Call signature | ||
interface I1 { | ||
(p1: A): p1 is C; | ||
} | ||
|
||
// The parameter index and argument index for the type guard target is matching. | ||
// The type predicate type is assignable to the parameter type. | ||
declare function isC_multipleParams(p1, p2): p1 is C; | ||
if (isC_multipleParams(a, 0)) { | ||
a.propC; | ||
} | ||
|
||
// Methods | ||
var obj: { | ||
func1(p1: A): p1 is C; | ||
} | ||
class D { | ||
method1(p1: A): p1 is C { | ||
return true; | ||
} | ||
} | ||
|
||
// Arrow function | ||
let f1 = (p1: A): p1 is C => false; | ||
|
||
// Function type | ||
declare function f2(p1: (p1: A) => p1 is C); | ||
|
||
// Function expressions | ||
f2(function(p1: A): p1 is C { | ||
return true; | ||
}); | ||
|
||
// Evaluations are asssignable to boolean. | ||
declare function acceptingBoolean(a: boolean); | ||
acceptingBoolean(isA(a)); | ||
|
||
// Type predicates with different parameter name. | ||
declare function acceptingTypeGuardFunction(p1: (item) => item is A); | ||
acceptingTypeGuardFunction(isA); | ||
|
||
// Binary expressions | ||
let union2: C | B; | ||
let union3: boolean | B = isA(union2) || union2; | ||
|
||
//// [typeGuardFunction.js] | ||
var __extends = (this && this.__extends) || function (d, b) { | ||
for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; | ||
function __() { this.constructor = d; } | ||
__.prototype = b.prototype; | ||
d.prototype = new __(); | ||
}; | ||
var A = (function () { | ||
function A() { | ||
} | ||
return A; | ||
})(); | ||
var B = (function () { | ||
function B() { | ||
} | ||
return B; | ||
})(); | ||
var C = (function (_super) { | ||
__extends(C, _super); | ||
function C() { | ||
_super.apply(this, arguments); | ||
} | ||
return C; | ||
})(A); | ||
var a; | ||
var b; | ||
// Basic | ||
if (isC(a)) { | ||
a.propC; | ||
} | ||
// Sub type | ||
var subType; | ||
if (isA(subType)) { | ||
subType.propC; | ||
} | ||
// Union type | ||
var union; | ||
if (isA(union)) { | ||
union.propA; | ||
} | ||
if (isC_multipleParams(a, 0)) { | ||
a.propC; | ||
} | ||
// Methods | ||
var obj; | ||
var D = (function () { | ||
function D() { | ||
} | ||
D.prototype.method1 = function (p1) { | ||
return true; | ||
}; | ||
return D; | ||
})(); | ||
// Arrow function | ||
var f1 = function (p1) { return false; }; | ||
// Function expressions | ||
f2(function (p1) { | ||
return true; | ||
}); | ||
acceptingBoolean(isA(a)); | ||
acceptingTypeGuardFunction(isA); | ||
// Binary expressions | ||
var union2; | ||
var union3 = isA(union2) || union2; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be TypeReferenceNode,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't TypeReferenceNode for non-primitives only? How about primitive types?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I believe TypeNode is indeed what you want