* 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
65 lines
1 KiB
TypeScript
65 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;
|
|
}
|
|
}
|