ianstormtaylor / superstruct

A simple and composable way to validate data in JavaScript (and TypeScript).

Home Page:https://docs.superstructjs.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

`instance` causes a TypeScript error with abstract classes

matthew-dean opened this issue · comments

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.