Skip to content

Ship TypeScript types - #296

Merged
alex-friedl merged 2 commits into
masterfrom
feat/typescript-types
Nov 29, 2026
Merged

alex-friedl merged 2 commits into
masterfrom
feat/typescript-types

Conversation

@alex-friedl

@alex-friedl alex-friedl commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator

The package now ships its own TypeScript types, so @types/node-pushnotifications is no longer needed.

Why

The DefinitelyTyped package is stuck at 3.1.1. It doesn't know { id, type } destinations, FCM topics and conditions, or the 7.0 result shape. It types send() with a callback as returning void, and it pulls in firebase-admin 11. TypeScript prefers a package's own types over @types, so the bundled file takes over even where the old package is still installed.

What's typed

index.d.ts is hand-written next to index.js and uses export = with a merged namespace, matching module.exports = PN and the static constants. import PN, { FCM } from 'node-pushnotifications' and require both work. Settings, destinations, data and results are taken from the senders rather than the old types.

Settings that can't work are rejected: fcm needs serviceAccountKey or credential, and wns needs notificationMethod, since passing either replaces the defaults. A string data only type-checks for Web Push destinations, the only service that sends it as is. Ambiguous destinations such as a topic with an id fail to compile, as does the data.recipients removed in 7.0.

Results are a union keyed on method. Checking r.method === 'fcm' gives a FirebaseError with code, and 'webPush' gives the subscription and an optional statusCode (network and option errors don't have one). The README's stale-token example now filters on method first so it type-checks, and gained a Web Push 404/410 example.

data stays open because WNS and ADM pass extra fields through. Known fields are still checked, and the open parts use any index signatures so payloads typed as interfaces are accepted. Firebase and node-apn types are imported from those packages. Web Push shapes are defined inline, so there's no @types/web-push dependency.

Checks

npm run test:types compiles test/types/ and runs in CI. It covers the README and docs examples, @ts-expect-error cases that must fail, and exact type assertions with expect-type, so an accidental any fails the check. check-package.sh runs attw on the packed tarball and compiles a consumer against it with TypeScript 7.0 and 5.5, the oldest version supported. 5.5 is needed for the README's narrowing in filter().

Closes #193

🤖 Generated with Claude Code

@types/node-pushnotifications is stuck at 3.1.1: no { id, type } or topic
destinations, no 7.0 result shape, and it pulls in firebase-admin 11.
Bundled types stay in sync with the code and take precedence over @types.

Type tests in test/types run in CI, and check-package compiles a consumer
against the packed tarball with TypeScript 7 and 5.9.

Closes #193

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@alex-friedl alex-friedl self-assigned this Nov 29, 2026
- Web Push errors can be plain errors, so the HTTP fields are optional
- String data only type-checks for Web Push destinations
- fcm settings need a credential, wns settings need notificationMethod
- Reject ambiguous destinations and the removed data.recipients
- Accept readonly arrays, array locArgs and the APNs critical sound object
- Exact type assertions with expect-type, attw in check-package, and
  TypeScript 5.5 as the oldest checked version

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@alex-friedl
alex-friedl merged commit 64b2062 into master Nov 29, 2026
6 checks passed
@alex-friedl
alex-friedl deleted the feat/typescript-types branch November 29, 2026 22:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Update typescript definitions

1 participant