Giter VIP home page Giter VIP logo

Comments (2)

pterrien avatar pterrien commented on July 18, 2024 1

Once again, thank you for your super quick answer, the level of support on this repo is truly amazing. :)
That's a very straightforward and elegant solution indeed.

Many thanks, that really helps!

from typebox.

sinclairzx81 avatar sinclairzx81 commented on July 18, 2024

@pterrien Hi,

There's a few of ways to approach this, most of which comes down to how you want your plugin API to look, and how types contribute to inference elsewhere. Perhaps the following helps?

TypeScript Link Here

import { Type, Static, TSchema, TObject } from '@sinclair/typebox'

// if you want a specialized object type
export interface TPlugin<Config extends TSchema> extends TObject<{ config: Config }> { }

// this is a factory function for the type above
export function Plugin<Config extends TSchema>(config: Config): TPlugin<Config> {
  return Type.Object({ config })
}

// A and B are types of TPlugin
const A = Plugin(Type.Object({ x: Type.Number(), y: Type.Number() }))

const B = Plugin(Type.Object({ a: Type.String(), b: Type.String() }))

type A = Static<typeof A>

type B = Static<typeof B>

Additionally, would it be possible to restrict the type of schema? In my case the corresponding TS type would be TObject obviously.

You can constrain the generic Config parameter to be of TObject only in the following way.

TypeScript Link Here

import { Type, TObject } from '@sinclair/typebox'

export interface TPlugin<Config extends TObject> extends TObject<{ config: Config }> { }

export function Plugin<Config extends TObject>(config: Config): TPlugin<Config> {
  return Type.Object({ config })
}

const X = Plugin(Type.String()) // error: Argument of type 'TString' is not assignable to parameter of type 'TObject<TProperties>'.

Hope this helps!
S

from typebox.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.