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

Shared TS types & enhancement #101

Open
draylegend opened this issue Dec 27, 2023 · 1 comment
Open

Shared TS types & enhancement #101

draylegend opened this issue Dec 27, 2023 · 1 comment
Labels
enhancement New feature or request idea Suggest or ideas for new features

Comments

@draylegend
Copy link

Idea

I need types for life cycle hooks (entry points) like init or load. As far as I know there're two ways to get typesafety with typescript:

  • explicit by importing them directly from 'pengu-loader'. In this case the package should be published on npmjs or
  • implicit like sveltekit does.

Explicit

Exported interfaces from 'pengu-loader'

export interface InitFn {
  (context: Context): void;
}

Interfaces that can be exported from 'pengu-loader', too

export interface Context {
  socket: Socket;
  rcp: Rcp;
}

export interface Socket {
  observe<Data = any>(api: string, listener: Listener<Data>): DisconnectFn;
  disconnect: DisconnectFn;
}

export interface Listener<Data = any> {
  (event: Event<Data>): void;
}

export interface Event<Data = any> {
  data: Data; // or `data?: Data`
  uri: string;
  eventType: EventType;
}

export interface DisconnectFn {
  (): void;
}

export type EventType = 'Create' | 'Update' | 'Delete';

Usage

import { InitFn } from 'pengu-loader';

interface Data { id: string; gamerName: string }

export const init: InitFn = ctx => {
  ctx.socket.observe<Data>('some/api', event => {
    console.log(event.data.id, event.data.gamerName);
  });
};

Implicit

At this point we need some dev server running in the background that generates interfaces silently. I think about vite.

Usage

interface Data { id: string; gamerName: string }

export const init = ctx => {
  ctx.socket.observe<Data>('some/api', event => {
    console.log(event.data.id, event.data.gamerName);
  });
};

Obviously the first option is way simpler than the second one.

It'd be great to get some feedback, whether that fits the philosophy etc. or not.

@nomi-san
Copy link
Member

Planned to publish it to npm @pengu.lol/types or @types/pengu. Pengu TS core is currently using global implicit typed via plugins/src/types.d.ts. It's easy to support both two cases, but implicit typed entry points could not be possible.

Explicit exported module/namespace:

import type { InitFn } from '@pengu.lol/types'

export const init: InitFn = ctx => { /*...*/ }

Global implicit, like vite/client does:

/// <reference types="pengu/global" />

Or using tsconfig.json:

  "typeRoots": ["pengu/global"]

@nomi-san nomi-san added enhancement New feature or request idea Suggest or ideas for new features labels Dec 28, 2023
@nomi-san nomi-san changed the title typed functions Shared TS types & enhancement Dec 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request idea Suggest or ideas for new features
Projects
None yet
Development

No branches or pull requests

2 participants