Skip to content

Type error happend by using union type string literal value for $path from $bind. #39

@wh1tecat-nya

Description

@wh1tecat-nya

example code:

import { $bind } from "@sp2/updater";

type Hoge = {
  fuga: {
    key1: string;
    key2: string;
  };
};

const { $push, $path } = $bind<Hoge>();

$push($path("fuga", "key1"), "hoge"); // valid

const key = Math.random() > 0.5 ? "key1" : "key2";

$push($path("fuga", key), "hoge"); // $path is type error

I see here.
https://github.com/phenyl-js/sp2/blob/5c14b6d31cf228b8403c2f184787eb02cbe41c80/modules/format/src/common/bound-document-path.ts#L21-L28
https://github.com/phenyl-js/sp2/blob/5c14b6d31cf228b8403c2f184787eb02cbe41c80/modules/format/src/common/bound-document-path.ts#L151-L154

The $path method is generating some depth path type and this using like following codes.
K1 extends keyof T, K2 extends keyof T[K1] ...
However if type K[i] had union type value, looks like TS compiler is checking 'left side value can extends all of expanded union type value?'.
And i think this issue can be solved by using like following codes.
K1 extends Extract<K1, keyof T>, K2 extends Extract<K2, T[K1]> ...

But if resolved this issue, #37 Error is still showing...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions