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
{{ message }}
Repository navigation
Omnibus fresh object subtype reduction / contextual type issue #19236
// Problem 1: Error spans are useless in contextually-typed array/object literal casestypeKind='foo'|'ball';// Error is on declared identifier instead of bad elementconstka: Kind[]=['foo','ball','xxxx','foo','ball'];// Error is on declared identifier instead of bad keyconstko: {[s: string]: Kind}={'x': 'foo','y': 'xxxx','z': 'foo'};// Problem 2: Subtype reduction screwage in the presence of contextual typetypeOK={ok?: boolean};// No error??consto1: OK='??' ? {ok: 'wat'} : {};// Problem 3: Aliased subtype reduction screwageconstobj='??' ? {ok: 'wat'} : {};// No error??consto2: OK=obj;
Hypotheses:
Computing a union type in the presence of a contextual type is a waste of time
If we do this, we can produce good error spans?
Unioning { x: T } with a fresh { } should produce { x?: T }
Unioning a fresh object literal with { } should do something special
Unioning fresh { x: number, y: number } | { x: number } should produce { x: number, y?: number}
Aleksey-Bykov The issue is that { x: T } | {} is too weak. Anything is assignable to it because anything is assignable to {}. The idea is to instead infer the type { x: T? } which says that if there is an x property it must be of type T.
Activity
zpdDG4gta8XKpMCd commentedon Oct 17, 2017
pardon me, can we keep
{ x: T } | {}as it is:{ x: T } | {}? or what is the way to encode an empty (no properties) object?ahejlsberg commentedon Oct 17, 2017
Aleksey-Bykov The issue is that
{ x: T } | {}is too weak. Anything is assignable to it because anything is assignable to{}. The idea is to instead infer the type{ x: T? }which says that if there is anxproperty it must be of typeT.zpdDG4gta8XKpMCd commentedon Oct 17, 2017
it's sad that there is no place for a true empty object
{}which doesn't accept any propertieswe use it as yet another unit type in addition to null and undefined and []
zpdDG4gta8XKpMCd commentedon Oct 17, 2017
so the problem here is the syntax
{}is overloaded with semantics:{}should mean an object with no properties as it literally stands{}should mean a top type assignable from anythingwe need a way to distinguish the two Exact Types #12936