ianstormtaylor / superstruct

A simple and composable way to validate data in JavaScript (and TypeScript).
https://docs.superstructjs.org
MIT License
6.99k stars 224 forks source link

`instance` causes a TypeScript error with abstract classes #1192

Open matthew-dean opened 10 months ago

matthew-dean commented 10 months ago

According to documentation, instance() should be the equivalent of instanceof. However, it causes a TypeScript error when used with an abstract class.

See: https://stackblitz.com/edit/arktype-bug-srv9n6?file=demo.ts

This code fails:

import { type, instance, assert } from 'superstruct';

abstract class Node {}

class SubNode extends Node {}

export const shouldPass = type({
  node: instance(Node),
});

const sub = new SubNode();
const isNode = sub instanceof Node;

export const result = assert({ node: sub }, shouldPass);

Error is Argument of type 'typeof Node' is not assignable to parameter of type 'new (...args: any) => any'. Cannot assign an abstract constructor type to a non-abstract constructor type.

yeoffrey commented 3 months ago

Hi @matthew-dean, I see you made a commit for this. Do you want to submit a PR for this?