TypeScript/tests/cases/conformance/jsdoc/returnTagTypeGuard.ts
Nathan Shively-Sanders a6c5d50749
Allow type predicates in JSDoc (#26343)
* Allow type predicates

1. Parse type predicates. Note that they are parsed everywhere, and get
the appropriate error when used places besides a return type.
2. When creating a type predicate, correctly find the function's parameters
starting from the jsdoc return type.

* Fix type of TypePredicateNode.parent: add JSDocTypeExpression

* Update API baselines

* Handle JSDoc signature inside @type annotations

* Fix circularity when getting type predicates

Also move createTypePredicateFromTypePredicateNode closer to its use

* More cleanup based on review comments
2018-08-10 15:31:39 -07:00

66 lines
1 KiB
TypeScript

// @noEmit: true
// @allowJs: true
// @checkJs: true
// @lib: esnext
// @Filename: bug25127.js
class Entry {
constructor() {
this.c = 1
}
/**
* @param {any} x
* @return {this is Entry}
*/
isInit(x) {
return true
}
}
class Group {
constructor() {
this.d = 'no'
}
/**
* @param {any} x
* @return {false}
*/
isInit(x) {
return false
}
}
/** @param {Entry | Group} chunk */
function f(chunk) {
let x = chunk.isInit(chunk) ? chunk.c : chunk.d
return x
}
/**
* @param {any} value
* @return {value is boolean}
*/
function isBoolean(value) {
return typeof value === "boolean";
}
/** @param {boolean | number} val */
function foo(val) {
if (isBoolean(val)) {
val;
}
}
/**
* @callback Cb
* @param {unknown} x
* @return {x is number}
*/
/** @type {Cb} */
function isNumber(x) { return typeof x === "number" }
/** @param {unknown} x */
function g(x) {
if (isNumber(x)) {
x * 2;
}
}