Skip to content
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

Typescript typings #25

Open
makarov-roman opened this issue Aug 21, 2019 · 6 comments
Open

Typescript typings #25

makarov-roman opened this issue Aug 21, 2019 · 6 comments

Comments

@makarov-roman
Copy link

makarov-roman commented Aug 21, 2019

Hi,
it would be great to have typescript types.

@josdejong
Copy link
Contributor

Would be nice if you can crate typescript definitions, thanks for the offer Makarov. Can you create a pull request in https://github.com/DefinitelyTyped/DefinitelyTyped?

@dbachrach
Copy link

This might be useful for anyone else:

declare module "timesync" {
  type TimeSync = {
    destroy();
    now(): number;
    on(event: "change", callback: (offset: number) => void);
    on(event: "error", callback: (err: any) => void);
    on(event: "sync", callback: (value: "start" | "end") => void);
    off(event: "change" | "error" | "sync", callback?: () => void);
    sync();

    send(to: string, data: object, timeout: number): Promise<void>;
    receive(from: string, data: object);
  };

  type TimeSyncCreateOptions = {
    interval?: number;
    timeout?: number;
    delay?: number;
    repeat?: number;
    peers?: string | string[];
    server?: string;
    now?: () => number;
  };

  function create(options: TimeSyncCreateOptions): TimeSync;
}

@rgolea
Copy link

rgolea commented Dec 30, 2022

To complete @dbachrach's response, I've added the server side typings as well:

declare module "timesync" {
  type TimeSync = {
    destroy();
    now(): number;
    on(event: "change", callback: (offset: number) => void);
    on(event: "error", callback: (err: any) => void);
    on(event: "sync", callback: (value: "start" | "end") => void);
    off(event: "change" | "error" | "sync", callback?: () => void);
    sync();

    send(to: string, data: object, timeout: number): Promise<void>;
    receive(from: string, data: object);
  };

  type TimeSyncCreateOptions = {
    interval?: number;
    timeout?: number;
    delay?: number;
    repeat?: number;
    peers?: string | string[];
    server?: string;
    now?: () => number;
  };

  function create(options: TimeSyncCreateOptions): TimeSync;

  export = TimeSync;
}

declare module 'timesync/server' {
  import type { Request as ExpressRequest, Response as ExpressResponse } from 'express';
  import type { createServer as createHttpServer, Server } from 'http';
  function requestHandler(req: Request | ExpressRequest, res: Response | ExpressResponse): void;

  function createServer(): ReturnType<typeof createHttpServer>;

  function attachServer(server: Server, path?: string): void;
}

Could we implement this in a PR?

We just need to add a typings field in package.json and make sure we point to the .d.ts file:

{
    "name": "timesync",
    "typings": "./typings.d.ts",
    ...
}

@nonotnoah
Copy link

@rgolea I followed your steps but I can't get it to work for me. I'm trying to import for use with socket.io.

I did:

  • npm i timesync
  • added typings.d.ts to the npm package folder
  • changed package.json to point at ./typings.d.ts
  • added `import timesync from 'timesync' to my client.tsx file

at this point the import errors complaining about types goes away, but when I try to actually use the module with const ts = timesync.create, Intellisense complains that "'timesync' only refers to a type, but is being used as a value here."

It seems like timesync is using the type declarations in typings.d.ts, but isn't using node_modules/timesync for the actual code.

@crossan007
Copy link

crossan007 commented Sep 6, 2023

@nonotnoah I get the same issue.

@crossan007
Copy link

I took a different approach and refactored the whole source to TypeScript and configured tsc to emit type declarations https://github.com/crossan007/timesync

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

No branches or pull requests

6 participants