Advanced-Frontend / Just-Now-QA

社群百问百答

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Vue源码

WuChuanL opened this issue · comments

我在看Vue2.x源码的时候,发现文件是js文件,但是里面出现了:数据类型这种写法,我不是很懂,希望老师能讲解一下。
文件 src/core/vdom/create-element.js

export function createElement (
  context: Component,
  tag: any,
  data: any,
  children: any,
  normalizationType: any,
  alwaysNormalize: boolean
): VNode | Array<VNode> {
  if (Array.isArray(data) || isPrimitive(data)) {
    normalizationType = children
    children = data
    data = undefined
  }
  if (isTrue(alwaysNormalize)) {
    normalizationType = ALWAYS_NORMALIZE
  }
  return _createElement(context, tag, data, children, normalizationType)
}

我没有见过这种写法,这种写法为什么不会报错?为什么要这样写?

我在看Vue2.x源码的时候,发现文件是js文件,但是里面出现了:数据类型这种写法,我不是很懂,希望老师能讲解一下。
文件 src/core/vdom/create-element.js

export function createElement (
  context: Component,
  tag: any,
  data: any,
  children: any,
  normalizationType: any,
  alwaysNormalize: boolean
): VNode | Array<VNode> {
  if (Array.isArray(data) || isPrimitive(data)) {
    normalizationType = children
    children = data
    data = undefined
  }
  if (isTrue(alwaysNormalize)) {
    normalizationType = ALWAYS_NORMALIZE
  }
  return _createElement(context, tag, data, children, normalizationType)
}

我没有见过这种写法,这种写法为什么不会报错?为什么要这样写?

这是TypeScript的语法,可以学学Ts的知识

我在看Vue2.x源码的时候,发现文件是js文件,但是里面出现了:数据类型这种写法,我不是很懂,希望老师能讲解一下。
文件 src/core/vdom/create-element.js

export function createElement (
  context: Component,
  tag: any,
  data: any,
  children: any,
  normalizationType: any,
  alwaysNormalize: boolean
): VNode | Array<VNode> {
  if (Array.isArray(data) || isPrimitive(data)) {
    normalizationType = children
    children = data
    data = undefined
  }
  if (isTrue(alwaysNormalize)) {
    normalizationType = ALWAYS_NORMALIZE
  }
  return _createElement(context, tag, data, children, normalizationType)
}

我没有见过这种写法,这种写法为什么不会报错?为什么要这样写?

这是TypeScript的语法,可以学学Ts的知识

这里的文件类型是.js,不是.ts,为什么不会报错呢?

flowjs了解下,文件最上面有注释

flowjs了解下,文件最上面有注释

多谢解答,在很多地方问过,您的解答是最标准的。