为 CDN 使用类型

Use typings for a CDN

我想使用 CDN 访问 JSON 验证库,因为它应该快一点(它从 CDN 中最近的可用服务器获取文件)。

这是JSON验证库: https://github.com/epoberezkin/ajv#using-in-browser

它将我引导至此 CDN: https://cdnjs.com/libraries/ajv

所以我将其包含在我的 HTML 中:

<html>
<head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ajv/4.10.4/ajv.min.js" integrity="sha256-LtA3VfycAam30/5e2Fq1f2tg8eIiFMOVWp1NDd6jmUU=" crossorigin="anonymous"></script>
</head>

现在输入...我 运行 npm install --save-dev @types/ajv 并安装了它。

node_modules/@types/ajv/package.json:

{
  "_args": [
    [
      {
        "raw": "@types/ajv",
        "scope": "@types",
        "escapedName": "@types%2fajv",
        "name": "@types/ajv",
        "rawSpec": "",
        "spec": "latest",
        "type": "tag"
      },
      "C:\Users\si556577\Documents\SSWebApp\app\Iag.DI.Web.SupplierApp"
    ]
  ],
  "_from": "@types/ajv@latest",
  "_id": "@types/ajv@1.0.0",
  "_inCache": true,
  "_installable": true,
  "_location": "/@types/ajv",
  "_npmOperationalInternal": {
    "host": "packages-12-west.internal.npmjs.com",
    "tmp": "tmp/ajv-1.0.0.tgz_1482502603556_0.6872997884638608"
  },
  "_npmUser": {
    "name": "types",
    "email": "ts-npm-types@microsoft.com"
  },
  "_phantomChildren": {},
  "_requested": {
    "raw": "@types/ajv",
    "scope": "@types",
    "escapedName": "@types%2fajv",
    "name": "@types/ajv",
    "rawSpec": "",
    "spec": "latest",
    "type": "tag"
  },
  "_requiredBy": [
    "#DEV:/",
    "#USER"
  ],
  "_resolved": "https://registry.npmjs.org/@types/ajv/-/ajv-1.0.0.tgz",
  "_shasum": "4fb2440742f2f6c30e7fb0797b839fc6f696682a",
  "_shrinkwrap": null,
  "_spec": "@types/ajv",
  "_where": "C:\Users\si556577\Documents\SSWebApp\app\Iag.DI.Web.SupplierApp",
  "author": "",
  "bugs": {
    "url": "https://github.com/epoberezkin/ajv/issues"
  },
  "dependencies": {
    "ajv": "*"
  },
  "deprecated": "This is a stub types definition for ajv (https://github.com/epoberezkin/ajv). ajv provides its own type definitions, so you don't need @types/ajv installed!",
  "description": "Stub TypeScript definitions entry for ajv, which provides its own types definitions",
  "devDependencies": {},
  "directories": {},
  "dist": {
    "shasum": "4fb2440742f2f6c30e7fb0797b839fc6f696682a",
    "tarball": "https://registry.npmjs.org/@types/ajv/-/ajv-1.0.0.tgz"
  },
  "homepage": "https://github.com/epoberezkin/ajv#readme",
  "license": "MIT",
  "main": "",
  "maintainers": [
    {
      "name": "types",
      "email": "ts-npm-types@microsoft.com"
    }
  ],
  "name": "@types/ajv",
  "optionalDependencies": {},
  "readme": "ERROR: No README data found!",
  "repository": {
    "type": "git",
    "url": "git+https://github.com/epoberezkin/ajv.git"
  },
  "scripts": {},
  "typings": null,
  "version": "1.0.0"
}

并且它还将此添加到 package.json:

"devDependencies": {
    "@types/ajv": "^1.0.0",

在代码中我是这样使用的:

validateJSONSchema(json) {
    var ajv = new Ajv();
    var valid = ajv.validate(this.schema, json);
    if (!valid) {
        console.log(ajv.errors);
        return false;
    } else {
        return true;
    }
}

代码有效,但是在 vs 代码中我收到编译时错误:Cannot find name "Ajv"

如何让我的打字正常工作?当我在本地安装包而不是使用 CDN 时,我只使用过打字。使用 CDN 时,打字甚至可以工作吗?

ajvpackage.json 文件中指出 @types/ajv 现已弃用,并且类型包含在包本身中:

This is a stub types definition for ajv (https://github.com/epoberezkin/ajv). ajv provides its own type definitions, so you don't need @types/ajv installed!

但是,包本身包含的类型旨在像这样使用:

import * as Ajv from "ajv";
let ajv = new Ajv(...);

所以除非使用 import 语句,否则 TypeScript 不会对类型感到满意。

这意味着如果你想使用 CDN,你必须配置你的模块捆绑器来这样做。仅包括 CDN <script> 并使 Ajv 可作为全局可用是不够的。

如何操作取决于您使用的捆绑器。