TypeScrip: Oh, aquele sistema de tipo engraçado

Olá, meu nome é Dmitry Karlovsky e recentemente eu, junto com Artur Mukminov , conduzi um workshop onde mostrei como desenvolver tipofunções complexas por meio de testes . São 2 horas de programação severa. Então, como um teaser, pegue uma análise das curiosidades do sistema de tipos de texto datilografado.













Relacionamentos são difíceis



É muito fácil verificar se um tipo é um subtipo de outro usando o typoternary:







type IsAExtendsB = A extends B ? true : false
      
      





Classify, 2 4 :







  • [ A, '<:', B ]



    — A B.
  • [ A, ':>', B ]



    — B A.
  • [ A, '==', B ]



    — ( ).
  • [ A, '!=', B ]



    — .


, Equal Assert, , , . Assert , .







! ..



Object



object



— , , :







type boolean_is_Object = Assert<
    boolean extends Object ? true : false,
    true
>

type boolean_is_not_object = Assert<
    boolean extends object ? true : false,
    false
>
      
      





, , , :







type Object_vs_object = Assert<
    Classify< Object, object >,
    [ Object, '==', object ]
>
      
      





: (, boolean



) (, Object



), — (, object



), — .







, . Object



, object



.









, , — , :







type boolean_is_true_or_false = Assert<
    boolean,
    true | false
>
      
      





:







enum FL4 { Absurd, False, True, Unknown }

type FL4_is_union = Assert<
    FL4,
    | FL4.Absurd | FL4.False | FL4.True | FL4.Unknown
>
      
      





( ):







type Absurd_is_number = Assert<
    Classify< FL4.Absurd, number >,
    [ FL4.Absurd, '==', number ]
>
      
      





:







type Absurd_is_never_wtf = Assert<
    Classify< FL4.Absurd, 0 >,
    [ never, '<:', 0 ]
>
      
      





, , ? , !







type One_is_never_wtf = Assert<
    Classify< FL4.Absurd, 1 >,
    [ FL4.Absurd, ':>', never ]
>
      
      





, , !







, — , :







enum FL3 { Absurd, False, True }

type Absurd_is_not_Absurd = Assert<
    Equal< FL3.Absurd, FL4.Absurd > | false,
    false
>
      
      





, . , , , :







enum HappyDebugging {
    False = "True", 
    True = "False",
}

type True_extends_string = Assert<
    Classify< HappyDebugging.True, string >,
    [ HappyDebugging.True, '<:', string ]
>
      
      





, number



, string



.









, :







  • never



    . , .
  • unknown



    — . . unknown



    .


Mas o que está se aproximando deles? É sim any



!
Por um lado, é totalmente intercambiável com unknown



:







type unknown_is_any = Assert<
    unknown,
    any
>
      
      





Mas, por outro lado, como o gato de Schrödinger, é um subtipo never



(e, como consequência, de qualquer outro tipo de do unknown



) e não o é ao mesmo tempo:







type any_maybe_extends_never = Assert<
    any extends never ? true : false,
    true | false
>
      
      





Resumindo, ele any



quebra o fundo em todos os sentidos possíveis. O destino de quem fica cara a cara com ele é difícil ...













Todo o código do artigo.







Bom debugging pessoal!














All Articles