打字稿未检测到非法论点

时间:2018-10-22 00:40:02

标签: typescript

Typescript在下面的代码中未检测到任何错误。

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>) {

        }
    }    
}

test2的参数必须为Map<string, **number**>,并且函数testMap<string, **string**>传递给test2。参数类型不匹配,但是Typescript无法检测到此错误。

为什么打字稿不能检测到此错误?

打字稿版本:2.9.2。

tsconfig.json:

{
  "compilerOptions": {
    "module": "es2017",
    "target": "es2017",
    "lib": [
      "dom",
      "es2017"
    ],
    "noImplicitAny": false,
    "sourceMap": true,
    "moduleResolution": "node"
  },
  "include": [
    :
  ],
  "exclude":  [
    :
  ]
}

2 个答案:

答案 0 :(得分:2)

调用方法的方式可以更改board的值,因此TypeScript默认将word = 'headphones' board = '_' * len(word) guessed = [] while board != word: guess = input('Enter letter: ') if guess in guessed: print(f'{guess} has already been used!') else: lst = [i for i, v in enumerate(word) if v == guess] board = list(board) if lst: for i in lst: board[i] = guess else: print(f'{guess} was not in the word.') guessed.append(guess) board = ''.join(board) print(board) print('Congratulations, the word was {word}!') 设置为this类型,这将关闭类型检查。您可以通过创建一个称为this的伪参数来告诉TypeScript上下文:

any

答案 1 :(得分:1)

作为@PSL建议,我在noImplicitThis上将true设置为tsconfig.json。现在Typescript可以检测到错误了。