Remove undefined from Optional Properties When Inferring to Index Signatutres
#43086
declarefunctionfoo<T>(obj: {[x: string]: T}): T;declareconstx1: {a: string,b: number};declareconstx2: {a: string,b: number|undefined};declareconstx3: {a: string,b?: number};declareconstx4: {a: string,b?: number|undefined};leta1=foo(x1);// string | numberleta2=foo(x2);// string | number | undefinedleta3=foo(x3);// string | numberleta4=foo(x4);// string | number- First two are uncontroversial.
- Second two...we effectively see those are the same.
- Intent of last one isn't clear.
- Need
missing. - Half of the world's
undefined is missing and half is still undefined.
- Probably fine to skew towards not including
undefined. - Reminds of "safe
any" missing < undefined- ...
void < undefined? - Can you use
void?
- Hard to guess how breaky the world would be.
- The current "optimistic" strategy in the PR seems good. People who badly want to deal with
undefined in these cases may just want --noUncheckedIndexedAccess.
Exclude enum/number compatibility rule from comparable relation
#42472
- Last design meeting notes seemed inconclusive.
- Seems like convo in issue is positive - merge.
Efficiency in Narrowing Generics
- Need to sync up on rules.
- Makes no sense that a
T extends unknown can be checked against === "foo", but a bare T with no constraint is allowed. - If two type parameters are distinct, and neither references the other in a constraint, then they shouldn't be comparable.
- It seems questionable that you even want an error there. Can someone defend it?
- Can always argue that anything is fair game for
===, .includes, etc. But then you allow a big class of mistakes of relating unrelated entities. - Well we definitely like the
number <-/-> string errors, those aren't as questionable. - But most generics have no constraint, so if you allowed the comparison, you could accidentally allow comparisons against the wrong thing.
- Is comparability what we use for uncalled functions?
- Okay, what about creating intersections with generics?
- We end up with a huge number of types in certain codebases (compilers with unions?)
- For primitives, we can do this fast. For object types, we're very bad about it.
- What about the discriminant checks we recently did?
- Could potentially do that?
Remove
undefinedfrom Optional Properties When Inferring to Index Signatutres#43086
missing.undefinedismissingand half is stillundefined.undefined.any"missing<undefinedvoid<undefined?void?undefinedin these cases may just want--noUncheckedIndexedAccess.Exclude enum/number compatibility rule from comparable relation
#42472
Efficiency in Narrowing Generics
T extends unknowncan be checked against=== "foo", but a bareTwith no constraint is allowed.===,.includes, etc. But then you allow a big class of mistakes of relating unrelated entities.number <-/-> stringerrors, those aren't as questionable.