microsoft / microsoft/TypeScript

Structural Comparison of Circular Tuples

Aperta
#37,420 3 commenti 0 reazioni 1 assegnatario Vedi su GitHub

@weswigham ci sta già lavorando.

Dal 16/3/2020.

Bug Domain: Conditional Types Rescheduled
Lingua principale
Go
Stelle
111k
Fork
14.3k
Merge medio
1g 19h
PR unite (30g)
117

Descrizione

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!

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.