You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Lots of frameworks that do their own special cased values to make properties reactive/observable (MobX, Vue, Knockout).
Can specially handle these at the type system
This syntax can be broken down to two things:
C ? T : F
C = true or C = never implies T
C = false implies F
C = any or C = boolean implies T | F
X extends Y
The former seems reasonable.
The latter seems questionable outside of the first's context.
Originally, we just had one syntax: C extends D ? T : F
Why'd we break them out? Well, composability for type combinators.
But you can still build it all up!
typeIs<T,U>=typeIf<Cextendsboolean,T,F>=Cextendstrue ? T : FtypeNot<Textendsboolean>=If<T,false,true>typeAnd<C1extendsboolean,C2extendsboolean>=If<C1,C2,false>typeOr<C1extendsboolean,C2extendsboolean>=If<C1,true,C2>
What if you have T extends Foo was its own type operator?
declarefunctionf<T>(x: T,y: Textendsstring);
That example basically says "take a T, and if T is a string, y must be true, or false otherwise"!
You really need T to be narrowed to something that looks like an array in the true branch of the first conditional so that you can index in with number in T[number].
We can't say it just becomes T & any[] because you'd get any from T[number].
Need to be able to add to the constraint of a type parameter.
But if you use those And, Or, etc. combinators, it's not clear how exactly to flow the information about how types have been narrowed.
What about making T extends Foo its own syntax that can only be used in the conditional expression?
[[Lost context]]
Also want to be able to introduce new type parameters that are inferred from the pattern of the constraint.
Figuring out whether two regular expressions are subsets of others is hard; each regular expression type is only assignable to itself, not to any other.
Each method on a regular expression can now act as a type guard.
Means that string literal types can be narrowed!
But do people really write a lot of credit card string literals in their code?
Really comes up more with things like CSS, HTML attributes like data-.+
If you test with Regex1, Regex2, and Regex3, do you get Regex1 & Regex2 & Regex3`?
Yes.
We could use this in the compiler to get pseudo-nominal types.
And these regexes could actually verify the data as well.
Not that we use regular expressions to validate right now...
Could this make RegexMatchArray more accurate?
Potentially possible, kind of hard.
How do these work with mapped types like { [P in /some-regex/]: T }.
Would need regex index signatures.
Want that! But we have a lot of internal assumptions about number and string index signatures.
What about new features in regular expressions?
Should we only support a subset of regular expressions?
Either reimplement regexes, or rely on the runtime.
What about if these were more structural?
Would need some restrictions on the regex.
What does this add on top of nominality?
Really the type guards that you can get from string matching.
Conditional Types
#12424
[[Reiteration of points from #20724.]]
C ? T : F
C = true
orC = never
impliesT
C = false
impliesF
C = any
orC = boolean
impliesT | F
X extends Y
C extends D ? T : F
T extends Foo
was its own type operator?T
, and ifT
is a string,y
must betrue
, orfalse
otherwise"!T
to be narrowed to something that looks like an array in the true branch of the first conditional so that you can index in withnumber
inT[number]
.T & any[]
because you'd getany
fromT[number]
.And
,Or
, etc. combinators, it's not clear how exactly to flow the information about how types have been narrowed.T extends Foo
its own syntax that can only be used in the conditional expression?No, these should only appear from narrowed conditional type branches.
What about expression contexts?
T extends string ? { value: T } : {}
?Regex Validated Types
#6579
data-.+
Regex1
,Regex2
, andRegex3
, do you get Regex1 & Regex2 & Regex3`?RegexMatchArray
more accurate?{ [P in /some-regex/]: T }
.number
andstring
index signatures.Stringly Typed
Guest: Mark Marron
Suppose (with pseudo-syntax...)
Example:
Imagine if the following could generate the above?
The text was updated successfully, but these errors were encountered: