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

Fixed member completions for NoInfer-wrapped unions #60271

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
11 changes: 8 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1927,6 +1927,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
isTypeParameterPossiblyReferenced,
typeHasCallOrConstructSignatures,
getSymbolFlags,
unwrapNoInferType,
};

function getCandidateSignaturesForStringLiteralCompletions(call: CallLikeExpression, editingArgument: Node) {
Expand Down Expand Up @@ -16513,6 +16514,12 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return !!(type.flags & TypeFlags.Substitution && (type as SubstitutionType).constraint.flags & TypeFlags.Unknown);
}

function unwrapNoInferType(type: Type): Type;
function unwrapNoInferType(type: Type | undefined): Type | undefined;
function unwrapNoInferType(type: Type | undefined) {
return type && mapType(type, t => isNoInferType(t) ? (t as SubstitutionType).baseType : t);
}

function getSubstitutionType(baseType: Type, constraint: Type) {
return constraint.flags & TypeFlags.AnyOrUnknown || constraint === baseType || baseType.flags & TypeFlags.Any ?
baseType :
Expand Down Expand Up @@ -29677,9 +29684,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}

function getNarrowableTypeForReference(type: Type, reference: Node, checkMode?: CheckMode) {
if (isNoInferType(type)) {
type = (type as SubstitutionType).baseType;
}
type = unwrapNoInferType(type);
// When the type of a reference is or contains an instantiable type with a union type constraint, and
// when the reference is in a constraint position (where it is known we'll obtain the apparent type) or
// has a contextual type containing no top-level instantiables (meaning constraints will determine
Expand Down
3 changes: 3 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5426,6 +5426,9 @@ export interface TypeChecker {
/** @internal */ typeHasCallOrConstructSignatures(type: Type): boolean;
/** @internal */ getSymbolFlags(symbol: Symbol): SymbolFlags;
/** @internal */ fillMissingTypeArguments(typeArguments: readonly Type[], typeParameters: readonly TypeParameter[] | undefined, minTypeArgumentCount: number, isJavaScriptImplicitAny: boolean): Type[];

unwrapNoInferType(type: Type): Type;
unwrapNoInferType(type: Type | undefined): Type | undefined;
}

/** @internal */
Expand Down
1 change: 1 addition & 0 deletions src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5637,6 +5637,7 @@ export function getPropertiesForObjectExpression(contextualType: Type, completio
}

function getApparentProperties(type: Type, node: ObjectLiteralExpression | JsxAttributes, checker: TypeChecker) {
type = checker.unwrapNoInferType(type);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At first, I considered unwrapping it in the public getContextualType (and alikes). But since NoInfers are not unwrapped when they contain structured types etc it wouldn't really address everything.

My line of thinking was that the API user likely shouldn't be concerned with NoInfer types all that much and it would be simpler if they just wouldn't be observable by the API users. But currently they will always be - we can obtain { prop: NoInfer<A> }, then get that property and end up with a NoInfer type easily.

So it's pretty impractical to unwrap those in the API layer, there are just too many places where the user interacts with the types and, perhaps, my initial instinct that NoInfers should be hidden from the API users wasn't good anyway.

So I decided I might very well just uwrap it here since that's something than an external API user would have to do anyway. It feels utterly annoying to deal with those NoInfers though. I'd expect most of the "get me a type" calls have to be wrapped with this unwrapping call. It's such an easy mistake to make.

But, in general, NoInfer<Structured> types behave almost the same as their wrapped base types. So I'm thinking now that maybe it's just the union case that gets in a way as that's not a union per-se but yet it's also a union for actual practical purposes.

So perhaps a better fix would be to distribute NoInfer over unions? That has some drawbacks for huge unions as we'd produce an equally huge distributed output union though.

if (!type.isUnion()) return type.getApparentProperties();
return checker.getAllPossiblePropertiesOfTypes(filter(type.types, memberType =>
!(memberType.flags & TypeFlags.Primitive
Expand Down
2 changes: 2 additions & 0 deletions tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6349,6 +6349,8 @@ declare namespace ts {
* and the operation is cancelled, then it should be discarded, otherwise it is safe to keep.
*/
runWithCancellationToken<T>(token: CancellationToken, cb: (checker: TypeChecker) => T): T;
unwrapNoInferType(type: Type): Type;
unwrapNoInferType(type: Type | undefined): Type | undefined;
}
enum NodeBuilderFlags {
None = 0,
Expand Down
12 changes: 6 additions & 6 deletions tests/baselines/reference/narrowingNoInfer1.types
Original file line number Diff line number Diff line change
Expand Up @@ -139,21 +139,21 @@ test2({ type: 'a' as const }, { type: 'b' as const }, (thing) => {
> : ^^^^^^^
>thing.type : "a" | "b"
> : ^^^^^^^^^
>thing : NoInfer<{ type: "a"; }> | NoInfer<{ type: "b"; }>
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>thing : { type: "a"; } | { type: "b"; }
> : ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>type : "a" | "b"
> : ^^^^^^^^^
>"a" : "a"
> : ^^^

thing;
>thing : NoInfer<{ type: "a"; }>
> : ^^^^^^^^^^^^^^^^^^^^^^^
>thing : { type: "a"; }
> : ^^^^^^^^^^^^^^

} else {
thing;
>thing : NoInfer<{ type: "b"; }>
> : ^^^^^^^^^^^^^^^^^^^^^^^
>thing : { type: "b"; }
> : ^^^^^^^^^^^^^^
}
});

41 changes: 41 additions & 0 deletions tests/cases/fourslash/completionNoInfer1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/// <reference path='fourslash.ts' />

// @strict: true
// @jsx: preserve

// @Filename: /a.tsx
//// /// <reference path="/.lib/react18/react18.d.ts" />
//// /// <reference path="/.lib/react18/global.d.ts" />
////
//// interface A {
//// type: "a";
//// value: string;
//// }
////
//// interface B {
//// type: "b";
//// size: number;
//// }
////
//// type Union = A | B;
////
//// function accept1<T extends Union>(union: NoInfer<T>) {}
//// accept1({ /*1*/ });
//// accept1({ type: "a", /*2*/ });
////
//// function accept2<T extends Union>(arg: { prop: NoInfer<T> }) {}
//// accept2({ prop: { /*3*/ } });
//// accept2({ prop: { type: "a", /*4*/ } });
////
//// function Accept3<T extends Union>(props: NoInfer<T>) {}
//// <Accept3 /*5*/ />;
//// <Accept3 type="a" /*6*/ />;

verify.completions({
marker: ["1", "3", "5"],
exact: ["size", "type", "value"],
});
verify.completions({
marker: ["2", "4", "6"],
exact: ["value"],
});