Typescript does not detect any error in the code below.
interface testIF {
test(): void;
test2(map: Map<string, number>): void
}
function f(): testIF {
return {
test: function () {
let map: Map<string, string> = new Map();
this.test2(map); // Passing Map<string, string>
},
test2: function(map: Map<string, number>) {
}
}
}
The argument of test2 must be Map<string, **number**> and the function test passes Map<string, **string**> to test2. The argument type does not match, but Typescript does not detect this error.
Why Typescript does not detect this error?
Typescript version: 2.9.2.
tsconfig.json:
{
"compilerOptions": {
"module": "es2017",
"target": "es2017",
"lib": [
"dom",
"es2017"
],
"noImplicitAny": false,
"sourceMap": true,
"moduleResolution": "node"
},
"include": [
:
],
"exclude": [
:
]
}
noImplicitThiscompiler option in tsconfig. I believe without that (Can't explain why - probably a bug)thisis being implicitly typed asanywhen using the factory function syntaxnoImplicitThistotruecould detect the error. Could you make an answer? I will accept it.thischange? (Do you still get an error even if you use the method correctly?)noImplicitThis, the above code generates the errorTS2345: Argument of type 'Map<string, string>' is not assignable to parameter of type 'Map<string, number>'. Type 'string' is not assignable to type 'number'.. It looks the type ofthisistestIF.noImplicitThiscauses the compiler to interpretthisas the object's type. I thought it just caused the compiler to error out ifthiswas interpreted asany.