Skip to content

Using types in interfaces/complex types throws a Class error #37

@tsiege

Description

@tsiege

I'm trying to use typescript-is to provide runtime validation for a library that manipulates a custom AST that is serialized/deserialized from well structured JSON. I've written out the type definition using only interfaces and a few basic types. When trying to compile it throws the following error

NestedError: Failed to transform node at: /Users/tristan/code/mobiledoc-core/lib/index.ts:53:32
    at transformNodeAndChildren (/Users/tristan/code/mobiledoc-core/node_modules/typescript-is/lib/transform-inline/transformer.js:33:15)
    at ts.visitEachChild (/Users/tristan/code/mobiledoc-core/node_modules/typescript-is/lib/transform-inline/transformer.js:35:62)
    at visitNode (/Users/tristan/code/mobiledoc-core/node_modules/typescript/lib/typescript.js:60504:23)
    at Object.visitEachChild (/Users/tristan/code/mobiledoc-core/node_modules/typescript/lib/typescript.js:60778:59)
    at transformNodeAndChildren (/Users/tristan/code/mobiledoc-core/node_modules/typescript-is/lib/transform-inline/transformer.js:35:15)
    at ts.visitEachChild (/Users/tristan/code/mobiledoc-core/node_modules/typescript-is/lib/transform-inline/transformer.js:35:62)
    at visitNodes (/Users/tristan/code/mobiledoc-core/node_modules/typescript/lib/typescript.js:60555:48)
    at Object.visitEachChild (/Users/tristan/code/mobiledoc-core/node_modules/typescript/lib/typescript.js:60774:45)
    at transformNodeAndChildren (/Users/tristan/code/mobiledoc-core/node_modules/typescript-is/lib/transform-inline/transformer.js:35:15)
    at ts.visitEachChild (/Users/tristan/code/mobiledoc-core/node_modules/typescript-is/lib/transform-inline/transformer.js:35:62)
Caused By: Error: Classes cannot be validated. https://github.com/woutervh-/typescript-is/issues/3
    at Object.checkIsClass (/Users/tristan/code/mobiledoc-core/node_modules/typescript-is/lib/transform-inline/visitor-utils.js:23:19)
    at visitObjectType (/Users/tristan/code/mobiledoc-core/node_modules/typescript-is/lib/transform-inline/visitor-type-check.js:155:22)
    at visitType (/Users/tristan/code/mobiledoc-core/node_modules/typescript-is/lib/transform-inline/visitor-type-check.js:346:16)
    at visitTypeReference (/Users/tristan/code/mobiledoc-core/node_modules/typescript-is/lib/transform-inline/visitor-type-check.js:142:20)
    at visitType (/Users/tristan/code/mobiledoc-core/node_modules/typescript-is/lib/transform-inline/visitor-type-check.js:338:16)
    at visitTypeReference (/Users/tristan/code/mobiledoc-core/node_modules/typescript-is/lib/transform-inline/visitor-type-check.js:142:20)
    at visitType (/Users/tristan/code/mobiledoc-core/node_modules/typescript-is/lib/transform-inline/visitor-type-check.js:338:16)
    at ts.createFunctionDeclaration.ts.createBlock.propertyInfos.map (/Users/tristan/code/mobiledoc-core/node_modules/typescript-is/lib/transform-inline/visitor-type-check.js:100:23)
    at Array.map (<anonymous>)
    at VisitorUtils.setFunctionIfNotExists (/Users/tristan/code/mobiledoc-core/node_modules/typescript-is/lib/transform-inline/visitor-type-check.js:94:30)

Here are the type definitions

// Cards
type CardPayload = Record<string, any>
type Card = [string, CardPayload]

// Atoms
type Atom = [string, string, unknown]

// Markers
// [typeIdentifier, openMarkupsIndexes, numberOfClosedMarkups, text]
type TextMarker = [0, number[], number, string]
// [typeIdentifier, openMarkupsIndexes, numberOfClosedMarkups, atomIndex]
type AtomMarker = [1, number[], number, number]
type Marker = TextMarker | AtomMarker

// Sections Tags enums / unions
type MarkupSectionTag = 'aside' | 'blockquote' | 'pull-quote' | 'h1' | 'h2' | 'h3' | 'h4' | 'h5' | 'h6' | 'p'
type ListSectionTag = 'ul' | 'ol'

// Section Identifiers
type MarkupIdentifer = 1
type ImageIdentifer = 2
type ListIdentifer = 3
type CardIdentifer = 10

// Sections
type MarkupSection = [MarkupIdentifer, MarkupSectionTag, Marker[]]
type ImageSection = [ImageIdentifer, string]
type ListSection = [ListIdentifer, ListSectionTag, Marker[][]]
type CardSection = [CardIdentifer, number]
type Section = MarkupSection | ImageSection | ListSection | CardSection

// Markups
type MarkupTag = 'a' | 'b' | 'code' | 'em' | 'i' | 's' | 'strong' | 'sub' | 'sup' | 'u'

type Markup = [MarkupTag, string[] | undefined]

// Mobiledoc Interface
export interface Mobiledoc_0_3_2 {
  atoms: Atom[]
  cards: Card[]
  markups: Markup[]
  sections: Section[]
  readonly version: '0.3.2'
}

is<Mobiledoc_0_3_2>(data) //=> throws compilation error
is<Section>(data) //=> throws compilation error
is<Markup>(data) //=> throws compilation error
is<Card>(data) //=> ok
is<Atom>(data) //=> ok

The interesting thing is that if I make the Mobiledoc_0_3_2 only have sections and cards properties it still throws an error, however if I make them not an array of those types it seems to work fine. Perhaps Array is being seen as a class?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions