#Why is any assignable to string

1 messages · Page 1 of 1 (latest)

ocean fable
#

Based on my intuition I thought the following

type F = any extends string ? true : never;

would result in never but that's not the case. I thought that for the extends to be true every value of a given type would need to be assignable to the other type 🤔

knotty shale
#

pretty sure any "extends" everything and everything "extends" any

#

any any value can be assigned to string, any string value can be assigned to any

#

that's kinda the point of any

#

maybe you're thinking of unknown

ocean fable
#

Doesnt any contain all values though? So for example numbers wouldnt be assignable to string

#

or is any just an escape hatch? 🤔

knotty shale
#

any doesn't "contain" anything, it disables typechecking

#

it isn't typesafe

#

unknown is the typesafe, uh equivalent i guess

ocean fable
#

ah ok, so my understanding that it is a top type was incorrect. Thanks a lot @knotty shale 🙏

solid notch
#

yup

#

unknown is the top type, never is the bottom type, any is the escape hatch

knotty shale
#

any is the side type

solid notch
#

lol

crystal templeBOT
#
type T = any extends string ? 1 : 0;
//   ^? - type T = 0 | 1
solid notch
#

any is wild

knotty shale
#

eh i guess that makes sense with the original, since true | never = true

solid notch
#

right

viscid condor
#

any is the quantum type - its sometimes the top type and other times the bottom type

#

whichever is more permissive

knotty shale
#

so it's a switch

viscid condor
#
type A = keyof any;
type B = keyof unknown;
type C = keyof never;
#

!ts A B C

crystal templeBOT
#
type A = string | number | symbol /* 1:6 */``````ts
type B = never /* 2:6 */``````ts
type C = string | number | symbol /* 3:6 */```
viscid condor
#

its funny, it just swaps places between unknown and never in different contexts

#

so in this context its the same as never

#

in others its more like unknown

knotty shale
#

so like as a function argument, with contravariance, that would be emulating never?

#

mm that is definitely not the right word

viscid condor
#

another is with assignment, if its on the left hand side its like unknown, but if its on the right hand side its like never

#

as never is basically the same thing as as any

#
const a: string = 1 as never
#

!ts

crystal templeBOT
#
const a: string = 1 as never
viscid condor
#

its probably not formally implemented like this

#

but the observed behaviour is consistent with it

#

given that any predates both unknown and never

knotty shale
#

!resolved