garronej/tsafe

Error identifying equality creating z.custom

sonegobruno opened this issue · 4 comments

I encountered a problem identifying equality between types generated by z.custom on an object

General file
Captura de tela de 2023-11-16 09-19-04

Schema type inferred
Captura de tela de 2023-11-16 09-24-56

Type created
Captura de tela de 2023-11-16 09-24-59

It can be seen that when the schema assert is performed directly it passes without problems. However, when asserting within an object it presents an error even though it has the same typing.

I also noticed that when determining the attribute as optional the error disappears.

Hello @sonegobruno,

Yeah sorry, I have to work on the Equals type, it sometimes gives fals negative, it's still better that false equality.

The workaround I iplement when the Equal type chokes is:

{
  type Expected = Shema;
  type Got = z.infer<typeof statusShema>;

  assert<Expected extends Got ? true : false>();
  assert<Got extends Expected ? true : false>();
}

As long as your types does not involves function this is bullet poof. If your types involves function it might, in certain edge cases, yield false positive (deeming equals types that are not).

Also, quick tip, I encourage you to use anonymous scope {} for testing equality of your types. It makes the core easyer to reason about and enable to reuse the same generic names multiple times in a single file.

Olá@sonegobruno,

Sim, desculpe, tenho que trabalhar no Equalstipo, às vezes dá falso negativo, ainda é melhor aquela falsa igualdade.

A solução alternativa que implemento quando o tipo Equal é bloqueado é:

{
  type Expected = Shema;
  type Got = z.infer<typeof statusShema>;

  assert<Expected extends Got ? true : false>();
  assert<Got extends Expected ? true : false>();
}

Contanto que seus tipos não envolvam função, isso é um bullet poof. Se seus tipos envolvem função, ele pode, em certos casos extremos, gerar falsos positivos (considerando tipos iguais que não são).

Thanks this worked for me 👍

Glad the workaround does it for you!