Open ajafff opened 3 years ago
Hey there! I'm not 100% certain this is exactly the same but the error is the same and I'm using JSDoc for type annotations with assertions. Essentially, even with every name in the call target having an explicit type annotation, I still get the aforementioned error. Despite making the JSDoc annotations even more verbose, explicit type annotations still don't make the error go away.
Note, there is a somewhat cumbersome workaround but I would hate to have to use that everywhere. You can inline the type annotation in the @param
or @typedef
. Here's a playground with both examples and the workaround also inlined below for convenience:
/**
* @param {unknown} value
* @returns {asserts value is number}
*/
function assertNumber(value) {
if (typeof value !== "number") throw "a ball in the air";
}
/**
* @param {{ num: typeof assertNumber }} inlinedAssert
* @param {object} jsdocObjectAssert
* @param {typeof assertNumber} jsdocObjectAssert.num
*/
function paramExample(inlinedAssert, jsdocObjectAssert) {
// This works with no compiler error π!
inlinedAssert.num("asdf");
// This should probably work π€·? but throws the error mentioned above π’.
jsdocObjectAssert.num("zxcv");
}
/** @typedef {{ num: typeof assertNumber }} InlinedAssert */
/**
* @typedef ObjectAssert
* @property {typeof assertNumber} num
*/
function typedefExample() {
/** @type {InlinedAssert} */
const inlinedAssert = { num: assertNumber }
// This works with no compiler error π!
inlinedAssert.num("qwerty")
/** @type {ObjectAssert} */
const objectAssert = { num: assertNumber }
// Same error as above π’.
objectAssert.num("π€")
}
Interesting that the object literal inlined notation in @param
and @typedef
are treated differently than the expanded JSDoc form.
TypeScript Version: 4.2.0-dev.20210101
Search Terms: assertion jsdoc
Expected behavior:
No error, assertion call narrows
v
.Actual behavior:
isDeclarationWithExplicitTypeAnnotation
doesn't handleJSDocParameterTag
andJSDocPropertyTag
Code
Compiler Options
```json { "compilerOptions": { "noImplicitAny": true, "strictFunctionTypes": true, "strictPropertyInitialization": true, "strictBindCallApply": true, "noImplicitThis": true, "noImplicitReturns": true, "alwaysStrict": true, "esModuleInterop": true, "checkJs": true, "allowJs": true, "declaration": true, "experimentalDecorators": true, "emitDecoratorMetadata": true, "moduleResolution": 2, "target": "ES2017", "module": "ESNext" } } ```Playground Link: Provided