类数组

类数组(Array-like Object)不是数组类型,比如 arguments:

  1. function sum() {
  2. let args: number[] = arguments;
  3. }
  4. // index.ts(2,7): error TS2322: Type 'IArguments' is not assignable to type 'number[]'.
  5. // Property 'push' is missing in type 'IArguments'.

事实上常见的类数组都有自己的接口定义,如 IArguments, NodeList, HTMLCollection 等:

function sum() {
    let args: IArguments = arguments;
}