Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Contextual narrowing to object #48576

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 37 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25439,7 +25439,7 @@ namespace ts {
return !!(type.flags & TypeFlags.Instantiable && !maybeTypeOfKind(getBaseConstraintOrType(type), TypeFlags.Nullable));
}

function hasContextualTypeWithNoGenericTypes(node: Node, checkMode: CheckMode | undefined) {
function tryGetContextualTypeWithNoGenericTypes(node: Node, checkMode: CheckMode | undefined) {
// Computing the contextual type for a child of a JSX element involves resolving the type of the
// element's tag name, so we exclude that here to avoid circularities.
// If check mode has `CheckMode.RestBindingElement`, we skip binding pattern contextual types,
Expand All @@ -25449,7 +25449,7 @@ namespace ts {
(checkMode && checkMode & CheckMode.RestBindingElement ?
getContextualType(node, ContextFlags.SkipBindingPatterns)
: getContextualType(node));
return contextualType && !isGenericType(contextualType);
return contextualType && !isGenericType(contextualType) ? contextualType : undefined;
}

function getNarrowableTypeForReference(type: Type, reference: Node, checkMode?: CheckMode) {
Expand All @@ -25460,10 +25460,41 @@ namespace ts {
// control flow analysis an opportunity to narrow it further. For example, for a reference of a type
// parameter type 'T extends string | undefined' with a contextual type 'string', we substitute
// 'string | undefined' to give control flow analysis the opportunity to narrow to type 'string'.
const substituteConstraints = !(checkMode && checkMode & CheckMode.Inferential) &&
someType(type, isGenericTypeWithUnionConstraint) &&
(isConstraintPosition(type, reference) || hasContextualTypeWithNoGenericTypes(reference, checkMode));
return substituteConstraints ? mapType(type, t => t.flags & TypeFlags.Instantiable ? getBaseConstraintOrType(t) : t) : type;
if (checkMode && (checkMode & CheckMode.Inferential)) {
return type;
}

// Bail-out early if we don't have anything instantiable in the first place.
if (!someType(type, t => !!(t.flags & TypeFlags.Instantiable))) {
return type;
}

const hasGenericWithUnionConstraint = someType(type, isGenericTypeWithUnionConstraint);
// If we only care about the apparent types of a value's constraints, we should narrow based on the constraint.
if (hasGenericWithUnionConstraint && isConstraintPosition(type, reference)) {
return getTypeWithConstraintsSubstituted();
}

const contextualType = tryGetContextualTypeWithNoGenericTypes(reference, checkMode);
// If there's no contextual type, that's a signal that we don't need to perform any substitution.
// If there *is* a contextual type, but it has top-level type variables, then it's not appropriate to narrow on
// constraints since the original type may be inferred from.
if (!contextualType) {
return type;
}

// When we have a type parameter constrained to a union type, or unknown, we can typically narrow on the constraint to get better results.
const substituteConstraints =
hasGenericWithUnionConstraint ||
// When the contextual type is 'unknown', we may need to narrow for compatibility with non-null targets.
// This allows some parity with a constraint of '{} | null | undefined'.
(getBaseConstraintOfType(type) || unknownType) === unknownType && isEmptyObjectType(contextualType);

return substituteConstraints ? getTypeWithConstraintsSubstituted() : type;

function getTypeWithConstraintsSubstituted() {
return mapType(type, t => t.flags & TypeFlags.Instantiable ? getBaseConstraintOfType(t) || unknownType : t);
}
}

function isExportOrExportExpression(location: Node) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ export enum PubSubRecordIsStoredInRedisAsA {
>Object.keys : { (o: object): string[]; (o: {}): string[]; }
>Object : ObjectConstructor
>keys : { (o: object): string[]; (o: {}): string[]; }
>soFar : SO_FAR
>soFar : unknown

hasField: (fieldName: string | number | symbol) => fieldName in soFar
>hasField : (fieldName: string | number | symbol) => boolean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class B<T> {
>x : T

return x;
>x : T
>x : unknown
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
//// [contextualNarrowingFromUnknownToObjects.ts]
declare function keysOfEmptyObject(o: {}): string[];
declare function keysOfNonPrimitive(o: object): string[];

namespace implicitConstraints {
export function keyLengthsEqualUsingEmptyObjectFn<T>(a: T, b: T): [T, T] | undefined {
if (typeof a !== "object" || typeof b !== "object" || !a || !b) {
return undefined;
}
if (Array.isArray(a) || Array.isArray(b)) {
return undefined;
}
if (keysOfEmptyObject(a).length !== keysOfEmptyObject(b).length) {
return [a, b];
}
return undefined;
}

export function keyLengthsEqualUsingNonPrimitiveFn<T>(a: T, b: T): [T, T] | undefined {
if (typeof a !== "object" || typeof b !== "object" || !a || !b) {
return undefined;
}
if (Array.isArray(a) || Array.isArray(b)) {
return undefined;
}
if (keysOfNonPrimitive(a).length !== keysOfNonPrimitive(b).length) {
return [a, b];
}
return undefined;
}
}

// Explicit Constraints of 'unknown'
namespace explicitConstraintsOfUnknown {
export function keyLengthsEqualUsingEmptyObjectFn<T extends unknown>(a: T, b: T): [T, T] | undefined {
if (typeof a !== "object" || typeof b !== "object" || !a || !b) {
return undefined;
}
if (Array.isArray(a) || Array.isArray(b)) {
return undefined;
}
if (keysOfEmptyObject(a).length !== keysOfEmptyObject(b).length) {
return [a, b];
}
return undefined;
}

export function keyLengthsEqualUsingNonPrimitiveFn<T extends unknown>(a: T, b: T): [T, T] | undefined {
if (typeof a !== "object" || typeof b !== "object" || !a || !b) {
return undefined;
}
if (Array.isArray(a) || Array.isArray(b)) {
return undefined;
}
if (keysOfNonPrimitive(a).length !== keysOfNonPrimitive(b).length) {
return [a, b];
}
return undefined;
}
}


//// [contextualNarrowingFromUnknownToObjects.js]
var implicitConstraints;
(function (implicitConstraints) {
function keyLengthsEqualUsingEmptyObjectFn(a, b) {
if (typeof a !== "object" || typeof b !== "object" || !a || !b) {
return undefined;
}
if (Array.isArray(a) || Array.isArray(b)) {
return undefined;
}
if (keysOfEmptyObject(a).length !== keysOfEmptyObject(b).length) {
return [a, b];
}
return undefined;
}
implicitConstraints.keyLengthsEqualUsingEmptyObjectFn = keyLengthsEqualUsingEmptyObjectFn;
function keyLengthsEqualUsingNonPrimitiveFn(a, b) {
if (typeof a !== "object" || typeof b !== "object" || !a || !b) {
return undefined;
}
if (Array.isArray(a) || Array.isArray(b)) {
return undefined;
}
if (keysOfNonPrimitive(a).length !== keysOfNonPrimitive(b).length) {
return [a, b];
}
return undefined;
}
implicitConstraints.keyLengthsEqualUsingNonPrimitiveFn = keyLengthsEqualUsingNonPrimitiveFn;
})(implicitConstraints || (implicitConstraints = {}));
// Explicit Constraints of 'unknown'
var explicitConstraintsOfUnknown;
(function (explicitConstraintsOfUnknown) {
function keyLengthsEqualUsingEmptyObjectFn(a, b) {
if (typeof a !== "object" || typeof b !== "object" || !a || !b) {
return undefined;
}
if (Array.isArray(a) || Array.isArray(b)) {
return undefined;
}
if (keysOfEmptyObject(a).length !== keysOfEmptyObject(b).length) {
return [a, b];
}
return undefined;
}
explicitConstraintsOfUnknown.keyLengthsEqualUsingEmptyObjectFn = keyLengthsEqualUsingEmptyObjectFn;
function keyLengthsEqualUsingNonPrimitiveFn(a, b) {
if (typeof a !== "object" || typeof b !== "object" || !a || !b) {
return undefined;
}
if (Array.isArray(a) || Array.isArray(b)) {
return undefined;
}
if (keysOfNonPrimitive(a).length !== keysOfNonPrimitive(b).length) {
return [a, b];
}
return undefined;
}
explicitConstraintsOfUnknown.keyLengthsEqualUsingNonPrimitiveFn = keyLengthsEqualUsingNonPrimitiveFn;
})(explicitConstraintsOfUnknown || (explicitConstraintsOfUnknown = {}));
Loading