微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

[Typescript] namespace

`namespace` is manily for the left over from the days where we’d refer to libraries through a single global variable. With this in mind, let’s not give namespace too much more thought for Now.

For example:

// using $ as class calling static method
$.ajax({
  url: "/api/getWeather",
  data: {
    zipcode: 97201,
  },
  success: function (result) {
    $("#weather-temp")[0].innerHTML =
      "<strong>" + result + "</strong> degrees"
  },
})
// using $ as function call
$("h1.title").forEach((node) => {
  node.tagName // "h1"
})

 

In order to desribe the types for `ajax`, we use namespace

function $(selector: string): Nodelistof<Element> {
  return document.querySelectorAll(selector)
}
namespace $ {
  export function ajax(arg: {
    url: string
    data: any
    success: (response: any) => void
  }): Promise<any> {
    return Promise.resolve()
  }
}

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐