You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a JavaScript file using JSDoc to indicate the return type of a function. The function's return type is a kind of generic (DataContainer<NumberData>) but when I call the method from a TypeScript file the TypeScript compiler treats it as any.
// index.tsimportgetDataContainerfrom'./factory'constcontainer=getDataContainer()// tsc thinks this returns an any but it should be DataContainer<NumberData>console.log(container.data().value())console.log(container.someBogusMethod())// XXX should be tsc compiler error here but container is treated as any so we get a runtime exception instead
Expected behavior:
When compiling this code tsc --allowJs src/* --outDir build/ we expect the TypeScript compiler to give an error at container.someBogusMethod() since DataContainer<NumberData> has no method named someBogusMethod.
Actual behavior:
The build works because it is incorrectly treating container as an any.
This results in a runtime exception when we run it with node build/index.js
Ah! Thank you. Like you suggested I needed to import DataContainer with the template <T>. I also needed to break out the NumberDatatypedef into it's own JSDoc comment which I didn't realize. This works:
I have a JavaScript file using JSDoc to indicate the return type of a function. The function's return type is a kind of generic (
DataContainer<NumberData>
) but when I call the method from a TypeScript file the TypeScript compiler treats it asany
.TypeScript Version: 3.9.6
JSDoc, generic, template, any, not working
Code
Expected behavior:
When compiling this code
tsc --allowJs src/* --outDir build/
we expect the TypeScript compiler to give an error atcontainer.someBogusMethod()
sinceDataContainer<NumberData>
has no method namedsomeBogusMethod
.Actual behavior:
The build works because it is incorrectly treating
container
as anany
.This results in a runtime exception when we run it with
node build/index.js
Playground Link:
Related Issues:
#26883
The text was updated successfully, but these errors were encountered: