Skip to content

[BUG] NoCheck doesn’t work as expected when used in intersection type #72

@jirutka

Description

@jirutka

Describe the bug

const flavorTypeSym = Symbol.for('flavorType')
// https://spin.atomicobject.com/typescript-flexible-nominal-typing/
interface Flavoring<FlavorT> {
  [flavorTypeSym]?: FlavorT
}

export type Flavor<T, FlavorT> = T & NoCheck<Flavoring<FlavorT>>

interface Foo {
  id: Flavor<number, 'Foo'>
  name: string
}

function create(foo: Assert<Foo>): Foo {
  return foo
}

The id property is completely ignored.

const flavorTypeSym = Symbol.for('flavorType');

function create(foo) {
  if (typeof foo !== "object" || foo === null)
      throw new Error("Expected foo to be Foo");
  if (typeof foo.name !== "string")
      throw new Error("Expected foo.name to be a string");
  return foo;
}
export {};

But when I move NoCheck to the Flavoring type:

interface Flavoring<FlavorT> {
  [flavorTypeSym]?: NoCheck<FlavorT>
}

export type Flavor<T, FlavorT> = T & Flavoring<FlavorT>

it is not ignored and generates an invalid code:

        if (id_1.__@flavorTypeSym@801 !== undefined)
                   ^

SyntaxError: Invalid or unexpected token

Playground link
https://googlefeud.github.io/ts-runtime-checks/playground/?code=MYewdgzgLgBAZgGwIYDcQCcAqBPADgUwGVsBbGAXhmJICMQEA6ODACgHJFUMcC2BKAFAB6ITAAWUKLggAuERFwBLMAyRQQJRaBoArfMCgNQJIVDz4IwdItxQAtInwAPRTQT47YDcqQI7ZpTAAcyEBZSh8dDgkYHwYADFkNGtggB5EriwAPhgAbwEYGABtTmSeIlIAXQB+GQSk7gEAXwEBZ1wMWAC4jOTUzAAaeszMHMpMGAAyGAA5EABhMX0Aa3SGlKC1kays1vDI6NiEkBA8gphFABM63oxUsABXWkihtniTtl3CsCQSfDroBtmq04A8wAZFOAYFZ8Gp8CxmCA6gBBCAQSJQdInLJ8G4nM6FdD4KAPdBgeAnYFAA

Expected behavior
I would expect ts-runtime-checks to ignore just Flavoring<FlavorT> and assert that the id property exists and is number.

Additional context

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