Giter Club home page Giter Club logo

Comments (2)

dsluijk avatar dsluijk commented on September 24, 2024

I think I narrowed down the main (two) issue(s).

type Validating<T extends Types = Types> = {
  type: T;
  value: T extends Types.One ? string : never;
};

const Validating: Describe<Validating> = object({
  type: enums(Object.values(Types)),
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  value: dynamic((_, ctx): any => {
    const type: Types = ctx.branch.at(-2)?.type ?? Types.One;
    if (type === Types.One) {
      return string();
    } else {
      return literal(undefined);
    }
  }),
});

This works, but requires you to set any on the return type of the dynamic.

The second issue is when you make the value an array of strings, not a string.

type Validating<T extends Types = Types> = {
  type: T;
  value: T extends Types.One ? string[] : never;
};

const Validating: Describe<Validating> = object({
  type: enums(Object.values(Types)),
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  value: dynamic((_, ctx): any => {
    const type: Types = ctx.branch.at(-2)?.type ?? Types.One;
    if (type === Types.One) {
      return array(string());
    } else {
      return literal(undefined);
    }
  }),
});

This Gives you a cryptic type error, but this can be fixed by manually overriding the value type in the object like so:

const Validating: Describe<Validating> = object({
  type: enums(Object.values(Types)),
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  value: dynamic((_, ctx): any => {
    const type: Types = ctx.branch.at(-2)?.type ?? Types.One;
    if (type === Types.One) {
      return array(string());
    } else {
      return literal(undefined);
    }
  }) as unknown as Describe<Validating["value"]>,
});

Still don't really have a nice fix for this, but hopefully this helps someone else facing the same issue.

from superstruct.

morlay avatar morlay commented on September 24, 2024

Could use Discriminated Union instead

export function discriminatorMapping<
  D extends string,
  Mapping extends Record<string, TypeAny>
>(
  discriminator: D,
  mapping: Mapping
): Struct<Simplify<DiscriminatedUnion<D, Mapping>>, null> {
  return  dynamic<any>((v: any = {}) => {
    const discriminatorValue = (v as any)[discriminator];
    const matched = mapping[discriminatorValue];

    if (typeof (v as any)[discriminator] === "undefined" || !matched) {
      return object({
        [discriminator]: enums(Object.keys(mapping))
      });
    }

    return object({
      [discriminator]: literal(discriminatorValue),
      ...matched.schema
    });
  });
}


type DiscriminatedUnion<
  D extends string,
  Mapping extends Record<string, TypeAny>
> = ValueOf<{
  [K in keyof Mapping]: { [k in D]: K } & Infer<Mapping[K]>;
}>;

type ValueOf<T> = T[keyof T];
enum NetType {
  AIRGAP = "AIRGAP",
  DIRECT = "DIRECT",
}

const t: Struct<{ netType: NetType.AIRGAP } | { netType: NetType.DIRECT, endpoint: string  }> = discriminatorMapping("netType", {
    [NetType.AIRGAP]: object(),
    [NetType.DIRECT]: object({
      endpoint: string()
    })
  }
)

from superstruct.

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.