Skip to content

Can't use generic schema type in traitsΒ #180

@tommy-mitchell

Description

@tommy-mitchell

The docs say that traits can be typed with a generic parameter:

koota/README.md

Lines 711 to 727 in 52a79cb

Traits can have a schema type passed into its generic. This can be useful if the inferred type is not good enough.
```ts
type AttackerSchema = {
continueCombo: boolean | null
currentStageIndex: number | null
stages: Array<AttackStage> | null
startedAt: number | null
}
const Attacker = trait<AttackerSchema>({
continueCombo: null,
currentStageIndex: null,
stages: null,
startedAt: null,
})
```

However, this seems to be broken (v0.5.3)1:

import { trait } from "koota";

type MySchema = {
    foo: number;
    bar: string;
    baz: [number, string];
};

const MyTrait = trait<MySchema>({
//                    ~~~~~~~~  Type 'MySchema' is not assignable to type 'Record<string, never>'
    foo: 0,
    bar: "",
    baz: () => [0, ""],
});

satisfies can't be used as a workaround in this case either, due to objects requiring initializer functions:

const MyTrait = trait({
    foo: 0,
    bar: "",
    baz: () => [0, ""],
//  ~~~  Type '() => (string | number)[]' is not assignable to type '[number, string]'
} satisfies MySchema);

Footnotes

  1. TypeScript Playground ↩

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions