-
Notifications
You must be signed in to change notification settings - Fork 21
Add TypeScript type declarations #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Add TypeScript type declarations #15
Conversation
- Add index.d.ts with comprehensive type definitions for all exported classes - Include type declarations for Connection, NodeJSSerialConnection, and other connection types - Add types for Constants, Advert, Packet, BufferUtils, and CayenneLpp - Add type declarations for submodules (constants, buffer_reader, buffer_writer) - Update package.json to reference the types file This enables TypeScript users to have proper type checking and autocomplete when using the library.
|
What about migrating to typescript, or tsc with jsdoc comments? It will be easier to ensure there is no drift with the .d.ts files. |
| syncNextMessage(): Promise<void>; | ||
|
|
||
| // Message sending | ||
| sendTextMessage(publicKey: Buffer, text: string): Promise<void>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think Unit8Array would be better than Buffer - Buffer comes from node and isn't a web standard. Buffer extends Uint8Array
| // Events | ||
| on(event: 'connected', listener: () => void): this; | ||
| on(event: 'disconnected', listener: () => void): this; | ||
| on(event: number, listener: (data: any) => void): this; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we do better than any?
I'm not actually familiar with the event API, but we can use numeric and string literal overrides
on(event: MsgWaiting, (msg: MsgWaitingArgs):this
on(event: OtherEvent, (msg: OtherEventArgs): :this
| } | ||
|
|
||
| export class NodeJSSerialConnection extends Connection { | ||
| constructor(port: string); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doc comments on types are really useful, they appear in IDE hover/go-to-definition which usually finds .d.ts files instead of .js files.
I won't recommend copying them all from .js to .d.ts, that's just more data that can get out of sync. But it's another reason to move to native typescript.
|
|
||
| // Type definitions | ||
| export interface SelfInfo { | ||
| publicKey: Buffer; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type, txPower, maxTxPower, publicKey, advLat, advLogn, reserved, manualAddContacts, radioFreq, radioBw, radioSf, radioCr, name
meshcore.js/src/connection/connection.js
Lines 609 to 621 in 9a979df
| type: bufferReader.readByte(), | |
| txPower: bufferReader.readByte(), | |
| maxTxPower: bufferReader.readByte(), | |
| publicKey: bufferReader.readBytes(32), | |
| advLat: bufferReader.readInt32LE(), | |
| advLon: bufferReader.readInt32LE(), | |
| reserved: bufferReader.readBytes(3), | |
| manualAddContacts: bufferReader.readByte(), | |
| radioFreq: bufferReader.readUInt32LE(), | |
| radioBw: bufferReader.readUInt32LE(), | |
| radioSf: bufferReader.readByte(), | |
| radioCr: bufferReader.readByte(), | |
| name: bufferReader.readString(), |
| pubKeyPrefix: Buffer; | ||
| pathLen: number; | ||
| txtType: number; | ||
| senderTimestamp: number; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT: Use type alias's to give names to string-ish or number-ish things. It doesn't add any overhead (it's still just a number) but adds some documentation.
type EpochSeconds = number
senderTimestamp: EpochSeconds
| name?: string; | ||
| } | ||
|
|
||
| export interface Message { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a ContactMessage
| text: string; | ||
| } | ||
|
|
||
| export interface Channel { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
NIT ChannelInfo?
| secret: Buffer; | ||
| } | ||
|
|
||
| export interface Contact { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing contact fields
| static readonly SupportedCompanionProtocolVersion: number; | ||
|
|
||
| static readonly ResponseCodes: { | ||
| ContactMsgRecv: number; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
List all the constants, and use the number literals. Unfortunatly that basically means copying the whole constants file.
| } | ||
|
|
||
| // Type declarations for submodules | ||
| declare module '@liamcottle/meshcore.js/src/constants.js' { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you defining these sub-module exports? constants is exported from the index, buf reader & writer are not, and I'm assuming that's by design?
Adds TypeScript type declarations to support TS projects using this library.
Changes
index.d.tswith type definitions for exported classespackage.jsonwithtypesfieldWhat's included
Types for:
Tested with a TypeScript project, types correctly resolve for the main API methods.