发布

现在您已经按照本指南的步骤编写了声明文件,是时候将其发布到 npm 了。您可以通过两种主要方式将声明文件发布到 npm:

  1. 与你的 npm 包捆绑
  2. 在 npm 上发布到 @types 组织

如果您的类型是由您的源代码生成的,请使用您的源代码发布这些类型。TypeScript 和 JavaScript 项目都可以通过 declaration 生成类型。

否则,我们建议将类型提交给 DefinitelyTyped,后者会将它们发布到 npm 上的 @types 组织。

在你的 npm 包中包含声明

如果您的包有一个主 .js 文件,您还需要在您的 package.json 文件中指明主声明文件。将 types 属性设置为指向您的捆绑声明文件。例如:

{
  "name": "awesome",
  "author": "Vandelay Industries",
  "version": "1.0.0",
  "main": "./lib/main.js",
  "types": "./lib/main.d.ts"
}

请注意,"typings" 字段与 types 同义,也可以使用。

hFt685iXyXLLPo2IYoL9sFzvdF87aDOHkXKDDpIzNVPXFH7E3VsY0MHsFlWqHxwdUI9vPtc66CPJbT+iZi9tR4L0NauAJBeuwDI6UtgP95ctDc/rE+wikLk25RXvYp/BzU400EnPnBBCqQav5n8X+5/HdjAWgybkXDWJOB07+f9ZO6PcGDD9GqWoBcdmeC2+aVMQWw/NfTPLRPHpq/P8ExyFWhF51jK4mYUD61SXWMfHwPlXeQatD+TyXwskmui1gzEc60R444mY6Nm7f7PS9crlXNJ7HksPHU/sA0yds8k=

依赖

Fk7jL1BfwIk9WFsexcbPKG+2LdTjCLmaHHMhl+EPWfw8O1eWDRP5niXtU+M9OIznjEo2lgCvh6VgqYQnuPJYKEcHxd536tfprVYSODOSkzZC5I3LOahX7N9at/EFAWBLnSeKHpMtg7yKGBuH2Za8dEJCA1HezE65TpK7x6W8ZRGSNw3XmRzQ9LsaFrNwacYcTY3JS7T+cmaURoqYN32SNlcVEbJkkOqoTZIKb9evCEh0CPOB5c0RXXTDG/H3yfsq

{
  "name": "browserify-typescript-extension",
  "author": "Vandelay Industries",
  "version": "1.0.0",
  "main": "./lib/main.js",
  "types": "./lib/main.d.ts",
  "dependencies": {
    "browserify": "latest",
    "@types/browserify": "lat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VRme8PdJYRCvmu3bWFuWiU5GOtmp8l9Zz3axJeCOY6fuoZ5iYdnNWYexPQrFPwVKVRJM/

红旗

/// <reference path="..." />

X1mQQEcJz7tDQ99ehyVknTQBUn90xFadZRSXaHwzqAS+Rsbg2VT71OEEgIlyy6Pxg4pmrwBfTi+tsNdXaYOTv9KxCMiG9tqt+X3lm4uhMLbdXhrURtchwUcjHOEt5jnK

/// <reference path="../typescript/lib/typescriptServices.d.ts" />
....

F56I034gL+KhZAN2OQp2qIsd2FulVsuXWJRsjPxiubaGXTQxy3SPwHHFEwhqjtdG1xmDWMtcF8kcuYsFkR8nfw==

/// <reference types="typescript" />
....

mjGwB0oIQAbVDGZML1iAEYmPsz0nL8Fyz2U5pSCGaeS1/RAQ+BQpegGThRppaGic0XSAJt85l6SqVBhA5rh+WHsDcpbNEssITdx4fmgIdm7eqoY5oiqIGTChNP686RJFX8bJIKdcmDqwSzz+MreeFFiW1ye12RpeDdfHh/TaTUs=

封装依赖声明

ERYcpyy0JN13bzm3fpVrRbeN07S0Z8Zu27rXI5O7PfHK2yIDvfMzWaGqaA5g3j/QMuuCQfNK8V7S2OiwNXfVBw==

    mhgmCEl10JngyooOuQmMkBjDOQ3o9Zh5Wz2y9jiCYyKNGtI9xL8wI+A9V4j+G78kO05Ys8iO8aUEFm7nM6I1SY2nDbnFQoC59ZVsroJ6TlRDusFKIcHtxhJ2biKGpuBQcBiiAnLItz8eXu8VAEJHMyaggCJSDFwWNdqDm/ieaewtYKsrFE7REy13XQSRklofAGMsT0uCDRjTe7PooiZ1FjYEamqC4S09VC1a2ob0hvxpbkzKSMhLcTDO/nJhzx61aQWyMjYqVIrpyj6/KCArlQ==

typesVersions 的版本选择

VkXbRUQYhsar2wXo76x+hJZCvxoTBN0jvC9nnyq5jtDEpuSXYzVMPPoZZaT5A1CZfQST74kFw3kbyFuoQnW87yvgVgAodiHxRQ8nyPZVJVQ4V8zZ4gvTsWN00RiTnfkQi0KfM8zbSLwMkJ5pvQahSugAIC6GstjoZXAPwsAi6jyBLRZZBnl0Grsxfjbte0ybbMLEDb9O4zuxvnb90p7EqJJ6dcQ2K7GEhm+bysX25qI=

文件夹重定向(使用 *)

0Mjefc2nplDXcwnj6dAR/iyKsigV3N1+AezoVltKeRemU85V3GA23kxv7mNTNz1bl/h2+ZLxLgl7iAjJuJGqdZBv+nClkIQQKBa3/aqKzepEFZ+eT9HCAgoeRCNqgDjo

{
  "name": "package-name",
  "version": "1.0.0",
  "types": "./index.d.ts",
  "typesVersions": {
    ">=3.1": { "*": ["ts3.1/*"] }
  }
}

mSDPL/NoJFcYvOWJIVrrg/hto3isjqy0zYkfOLuIgaFoXlUEmiSgZS67lMJX4ZdCIvInjJwBlGYbfaaCWAf+3BHcJs0sqIsvjLGkTggiCuORvQoxpwP0tC4EPFW6VRINO9VoB1Ow4kH1QgZ7W1MJIEOxMW6QuWwaiOE9aiAGqRivy38fzAM9pj1nR886JTWoCe8QDmQ9flBAuzAM2iqGftnjLDOvmWSAUVTKYtr6+AXLzMTQ+1CmNbjEhbyYMD8E3/A9Miei8wzPezZ3v+EL3lntyi//m8xIXKd6oWTAgGrtlAP8VbnsghQSYHU8LAIs

QZRglR2iKxIbroi3XT70puomYKnroAwdFiGbAmYiano9BJ6DTfNq5ohL5kcFy9dmfwtzIm3eyL9wJTZuCyPb2FsM8bnHJ/ZFYlBMGOMJ4T2dtYkL7n0eDWC/p7B7bLL/WgQmT4wwa1Of0914MYh5xjl7/TDQyafzozAHkdkohq1xCw252+Olubnww04wF4MH7+5STasDhvqh5vP6T8JfmTjJH7Ne1kZqH3pUyanzHW8=

vUkqaHGqqRcIkIbFGPuYi1OzFQkWp1wXVPP81avBylvXfXpC+blqiZGw1I58H13/B8Yd6DP7OA/7HAFeVc+Aj3PrMJSi0UiPbhYBavc0pRXPTHOjP4a/XBbkMtfVtAnLDqHdHpiOltJv/hn3b/xTddM6Ag33VqwNMmvRxEcfo3P3QCNbquAc/GdDCWirY8seTRJ5SLGok7ELGRZZs6tW20P6JJfiaIXq8wOt/Tsp4o0D5KhVJqPwmMSxD2y4BNtREZCWfo8Z/79J63W82iKpgs7DvREsuIZ4ifj0fyLxHZFXPdJDTogsAXl0TrpYxWFskIA6VF25PBZPRy6/F9ME4oDwSH6THOSRUEBCdnfuIjzORVBeYSI5K0j3+dafWbpUwNGt+3Bj7Y0OAnrjZ9LXyXi3iYPG2UrTERKaBiE0cE/VatFCl4yaqzA5xPlhRtRSUyfOpGHBC1II2nRmTzK8drWcco9sozGb48AE70c60UoVCRM6UgjE7lmktuSYE8tXAGd628K2AqwcO6XsomPPUX2zCLmeK+hXfVUzqNmDx2imL8n5T+qptVWLftAEs7VFHIoDnlhQZla77cIVwVvVeA==

f3Cuxf+E8Xc50YCR6GJizPPDZmEGelC9ojTgFG8+SkDbHS0WlpBPNWM3Zy38oF+fpH1kggDc0m0rm9wvLZizbF8UJi6e9Xf9nKdSuKeJIhN6EJO7zfxD6KMg8RXSqlEDK6cM/gT4YRjG8F2NsnrSnKRKjEqdnWG/0qGEtnSvcgn/joxDi0IwDTmmeWMh2uTZ/3U8J3THoqtbovk7GGMqXCn4huyD6/RSkGbQsvbX343spJvsQud7gyLLEkBJehmPylf2CsIKn6ydoJryM0BfzUrFkscK2WVTwrUGMY/+FLLN7VRtJPqnMRwqMUuVC8cunu8BU85k6HAB+c2N+bmTCKEWx3olc05ODM7yh/s1hbGsMP6XdQ1ebQj5X99B8YoQOmHktf5OFB1MQCuL2r/LGHyf1lVLMRVhLS23jbGZqXm3bMcge0AFFuidLHTX9xWW

文件重定向

gC8IB4hfWYfTmJ2QqlyF8uO3ump1tuHm4kcQmuaUBLTvp8a0hz8HYZ1oHRLwt4QvG9FmFQ4zOpERDtx72ZMjemat1gMKbl618lNwqa/V22uCHFQwoorlVldJwxYJ80GCm/mn+3FUHwYuJfDjO6YjdWqGO5EU3435XR0rH+DP0nuSVITZ1Jmlpndi/bsKTMYu

{
  "name": "package-name",
  "version": "1.0.0",
  "types": "./index.d.ts",
  "typesVersions": {
    "<4.0": { "index.d.ts": ["index.v3.d.ts"] }
  }
}

KWQkPoLPvXZwbh8YcYLjt6pF9WWUEv4iUxYA1id2R3KjU04jiwI4IhnE8y/eTnQX9Y3l2wh88ZlNHAD4Wpia2Ov9VXBYfrurCLv/sAyVNX5kWq57kdu9e+iEDNJNtcqKY71yMGhntCAElpaX40zi6OzaZCzmUoZ1PRtyWDSzMl8Vj5FUaB74DnXSjRsErIRVZgl58r5cLKMQlNrYYaAyjYT2/zh0jOfQq9Ad+Rd63jkXONZzdMVtikG/7tmRYbByFVpfen7jlm1jDfIyXehjmQ==

匹配行为

5rn9jgKklg9pGOjzErvyE0Be3fz2W6WOtidq18cB+Wjqi79qeBpExluWoDvnKM7GmLNt/m3MO96KHOuOS4Hs50WSt2rOMkrLwFOvfc3tFQ0QqpAYmmiXqmcbc4Kew5km6dw0zX8d/1D3S5WjEmYspynA1mjZozivmjc0TYN37EQv3m9psuJiWgcqVOYv6QzDks4l+god3XFb2d8PZbOxww==

多个字段

XZGou50iWkmKF6M6ft+SP5kCaiYURftFe1E18gpie7zNXscsD1dckhIau4RCd1juhSeqHRmeVRghT/Mwh/aPYKVucBhnDW2Bg1ZJ8UZypp/NRJ6CcZEli/+nPuR3lPZXUZAQJ0PIReYVDT6RyxptAw==

{
  "name": "package-name",
  "version": "1.0",
  "types": "./index.d.ts",
  "typesVersions": {
    ">=3.2": { "*": ["ts3.2/*"] },
    ">=3.1": { "*": ["ts3.1/*"] }
  }
}

ASZfI8WJtxG4AAIaien2pdc15YJockmi6x11f6d+pPk2n8+9JNdHXtvU2N7e01v+FMlPJDcyeRVzmGVGIeRfA/ju5S2ZI/dA56EuHPKqKp6y+ahGr7FgehQwqz1DwOydsjmEq7VQgEdCuhCuw70zV0x/NryeTRfCkKTux29I6Z3WnhuMgJcEmizUKjvwWIGwrQ4mFTw96hDmZvuzF0yjMXcqgPOOrGmj9gtPQwPJ4rp5YXQU75qEsUtYyd0JGrOE3ysp1zb3PxrRAC1ACUoI2s51BjYUgc1QDbTNKk5nwCNR/Mh9nmNvt0ykqfz3XRNzEs7M7YIYrWfUyDS13sZg3fY4572Hmy7IMRMlgetn5B7nJ98ZR4YUEqMa1YVaF21T7zgQnqpJNAyHIyodN8EKQvNLyfIr1q5caCeA+DYSFNrTjuTah90+MNFO0earS6JO

{
  "name": "package-name",
  "version": "1.0",
  "types": "./index.d.ts",
  "typesVersions": {
    // NOTE: this doesn't work!
    ">=3.1": { "*": ["ts3.1/*"] },
    ">=3.2": { "*": ["ts3.2/*"] }
  }
}

发布到 @types

WkelM3VBDkIKYG4gh4iRVPBPh5kphCklF4VExaOuOFsR2VmtbjkitG5ezuBLjB7OPzVPu28I42fYx0U6c/Mdr4DFcvw2NUfiesJKSCZBJw5dFQckQ7o7JpUV+T2WKiTuayF1ouDIqJp4TBb/uhvnndb7QeHvBg5oqrbedG9PdYCitJXAfasRKAfWwIovfLgEv/TyTx7oFenYRwkf/9L+P51WlBbNGMzf8isP9/VUMwltVktAaJfQaNpOnJpnZ8a8nC5JoBHtStmxhMi1N5sNHedhsu/lhNmr5+yrktcdmJlVftxrKGgIY1g+ztORMH7PbhISN5/qTT4OopaD1oO8iKSOiva4Iw+UFQUfTwYETyWkZevhxsNQGhIVTNWUzcneoKmZrOKkBQv0C4bBCR4RP7Lp45reiUKV33ppLL6nNYgjlFwfTopDBLOG+amI9YrMl1rOJzUCXPufN0g+1lFbzQBuJee/aqT1AmlgG12E6DDKkQ31XB3OH4LvL5qvfM5Wxyoj/w9gPawSr8I1wl8Zrb30NZqjGJ36arTWEHK/a71TO6zj88GkFR860OjtQRnWkMp4CewkettOEA9ahbGhU1U4tTAEYAvO0YiwNhie7Uot4BdoCCmWzLq+Sf4YyW+iLrv23Zug2QMcG8tHwxq/oQ==