Skip to content
This repository was archived by the owner on Feb 9, 2024. It is now read-only.

Conversation

@ispivey
Copy link
Contributor

@ispivey ispivey commented Jun 13, 2020

In order to publish types, the minimal change required is:

  • Update index.ts to also export Options, CacheControl, MethodNotAllowedError, NotFoundError, InternalError
  • npm run build (which runs tsc -d, where the -d flag indicates to
    generate type definition files)
  • Cut a new release

I observed that this generates a dist/index.d.ts type definition file that
contains all types except those defined in src/mock.ts, which are only
required for package tests and thus don’t need to be published.

package.json already specifies these as the package’s types:

"main": "dist/index.js",
"types": "dist/index.d.ts",

As a side note, I’m not sure why index.d.ts (even with a subset of types)
isn’t included in the 0.0.10 release. If I check out the v0.0.10 tag and run
npm publish —dry-run I observe that it includes dist/index.d.ts in the
package. However, that file is not present in the built package from npm.

Finally, I tested that this works using npm link – reviewers can play along
too if you like. First, in this branch of kv-asset-handler, run npm link.

Then, I created a new typescript project containing the updated sample code
from @kentonv’s PR againstREADME.md in #102, then ran:

npm i @cloudflare/kv-asset-handler --save
npm link @cloudflare/kv-asset-handler

This symlinks to the local copy of kv-asset-handler instead of the 0.0.10
release on npm. After doing so, I was able to do the following in said new
project:

import { getAssetFromKV, NotFoundError, MethodNotAllowedError } from '@cloudflare/kv-asset-handler'

I validated that the new example code transpiles when using these typings.

In order to publish types, the minimal change required is:
* Update `index.ts` to also export `Options, CacheControl,
  MethodNotAllowedError, NotFoundError, InternalError` `npm run build` (which
  runs `tsc -d`, where the `-d` flag indicates to generate type definition
  files)

I observed that this generates a `dist/index.d.ts` type definition file that
contains all types except those defined in `src/mock.ts`, which are only
required for package tests and thus don’t need to be published.

`package.json` already specifies these as the package’s types:
```json
"main": "dist/index.js",
"types": "dist/index.d.ts",
```

As a side note, I’m not sure why `index.d.ts` (even with a subset of types)
isn’t included in the `0.0.10` release. If I check out the `0.0.10` tag and run
`npm publish —dry-run` I observe that it includes `dist/index.d.ts` in the
package.

Finally, I tested that this works using `npm link`. I created a new typescript
project containing the updated sample code from @kentonv’s PR against
`README.md` in [Fix invalid JavaScript in readme example by kentonv · Pull
Request #102 · cloudflare/kv-asset-handler ·
GitHub](https://github.com/cloudflare/kv-asset-handler/pull/102/files#diff-04c6e90faac2675aa89e2176d2eec7d8),
then ran:

```bash
npm i @cloudflare/kv-asset-handler --save
npm link @cloudflare/kv-asset-handler
```

This symlinks to my local copy of `kv-asset-handler` instead of the 0.0.10
release on npm. After doing so, I was able to do the following in said new
project:

```typescript
import { getAssetFromKV, NotFoundError, MethodNotAllowedError } from '@cloudflare/kv-asset-handler'
```

I validated that the new test code transpiles and runs when using these typings.
@ispivey ispivey linked an issue Jun 13, 2020 that may be closed by this pull request
@ispivey ispivey requested review from 1000hz and kentonv June 13, 2020 14:40
@1000hz
Copy link

1000hz commented Jun 15, 2020

As a side note, I’m not sure why index.d.ts (even with a subset of types)
isn’t included in the 0.0.10 release.

not sure why you're not seeing it, but it is:
https://unpkg.com/browse/@cloudflare/[email protected]/dist/index.d.ts

Copy link
Contributor

@EverlastingBugstopper EverlastingBugstopper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rubber stamp from me since cina + kenton already 👍

@ispivey ispivey merged commit 6282601 into master Jun 15, 2020
@ispivey ispivey added this to the 0.0.11 milestone Jun 24, 2020
@kristianfreeman kristianfreeman deleted the types-take-two branch May 2, 2022 15:37
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TypeScript declarations on npm

4 participants