microsoft / microsoft/TypeScript

Structural Comparison of Circular Tuples

Open
#37,420 3 comments 0 reactions 1 assignee View on GitHub

@weswigham is already working on this.

Since Mar 16, 2020.

Bug Domain: Conditional Types Rescheduled
Dominant language
Go
Stars
111k
Forks
14.3k
Avg merge
2d 4h
Merged PRs (30d)
132

Description

TypeScript Version: 3.8.2

Search Terms: structure, resolve, compare, equality, type, mismatch, circular, tuple, cycle, recursive

Code

I'm trying to represent some type information for runtime use. I define the following enum:

enum Type {
  Int = "Int",
  List = "List",
}

And I define a Codec type: a tuple, within which the first element is the given Type enum value, and the second element––which is optional––is another Codec:

type Codec<
  T extends Type,
  C extends Codec<Type> | undefined = undefined
> = C extends undefined ? [T] : [T, C];

I'm able to assign Codecs as expected:

const c1: Codec<Type.Int> = [Type.Int];
const c2: Codec<Type.List, Codec<Type.Int>> = [Type.List, [Type.Int]];

And I'm able to create helpers for the Type.Int Codec:

type IntCodec = Codec<Type.Int>;
const createIntCodec = (): IntCodec => [Type.Int];
const intCodec = createIntCodec(); // signature is `[Type.Int]`

However, I'm unable to create helpers for Codecs which nest other Codecs:

type ListCodec<C extends Codec<Type>> = Codec<Type.List, C>;
const createListCodec = <C extends Codec<Type>>(of: C): ListCodec<C> => [Type.List, of]; // error

This results in an error: Type '[Type.List, C]' is not assignable to type 'Codec<Type.List, C>'. ts(2322).

Playground Link

Any thoughts would be greatly appreciated! Thank you!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.