Skip to content

fix: filtering promise properties in object literal completion #59316

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

Merged
Merged
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1637,6 +1637,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
getParameterIdentifierInfoAtPosition,
getPromisedTypeOfPromise,
getAwaitedType: type => getAwaitedType(type),
getAwaitedTypeOfPromise: type => getAwaitedTypeOfPromise(type),
getReturnTypeOfSignature,
isNullableType,
getNullableType,
Expand Down Expand Up @@ -1861,6 +1862,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const node = getParseTreeNode(nodeIn);
return node && tryGetThisTypeAt(node, includeGlobalThis, container);
},
filterType,
getTypeArgumentConstraint: nodeIn => {
const node = getParseTreeNode(nodeIn, isTypeNode);
return node && getTypeArgumentConstraint(node);
Expand Down
3 changes: 3 additions & 0 deletions src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4973,6 +4973,7 @@ export interface TypeChecker {
getPromisedTypeOfPromise(promise: Type, errorNode?: Node): Type | undefined;
/** @internal */
getAwaitedType(type: Type): Type | undefined;
getAwaitedTypeOfPromise(type: Type): Type | undefined;
/** @internal */
isEmptyAnonymousObjectType(type: Type): boolean;
getReturnTypeOfSignature(signature: Signature): Type;
Expand Down Expand Up @@ -5288,6 +5289,8 @@ export interface TypeChecker {
tryGetThisTypeAt(node: Node, includeGlobalThis?: boolean, container?: ThisContainer): Type | undefined;
/** @internal */ getTypeArgumentConstraint(node: TypeNode): Type | undefined;

filterType(type: Type, f: (t: Type) => boolean): Type;

/**
* Does *not* get *all* suggestion diagnostics, just the ones that were convenient to report in the checker.
* Others are added in computeSuggestionDiagnostics.
Expand Down
5 changes: 3 additions & 2 deletions src/services/completions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5472,9 +5472,10 @@ function getJsDocTagAtPosition(node: Node, position: number): JSDocTag | undefin
/** @internal */
export function getPropertiesForObjectExpression(contextualType: Type, completionsType: Type | undefined, obj: ObjectLiteralExpression | JsxAttributes, checker: TypeChecker): Symbol[] {
const hasCompletionsType = completionsType && completionsType !== contextualType;
const promiseFilteredContextualType = checker.filterType(contextualType, t => !checker.getAwaitedTypeOfPromise(t));
const type = hasCompletionsType && !(completionsType.flags & TypeFlags.AnyOrUnknown)
? checker.getUnionType([contextualType, completionsType])
: contextualType;
? checker.getUnionType([promiseFilteredContextualType, completionsType])
: promiseFilteredContextualType;

const properties = getApparentProperties(type, obj, checker);
return type.isClass() && containsNonPublicProperties(properties) ? [] :
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 @@ -6070,6 +6070,7 @@ declare namespace ts {
getBaseTypes(type: InterfaceType): BaseType[];
getBaseTypeOfLiteralType(type: Type): Type;
getWidenedType(type: Type): Type;
getAwaitedTypeOfPromise(type: Type): Type | undefined;
getReturnTypeOfSignature(signature: Signature): Type;
getNullableType(type: Type, flags: TypeFlags): Type;
getNonNullableType(type: Type): Type;
Expand Down Expand Up @@ -6217,6 +6218,7 @@ declare namespace ts {
isArrayLikeType(type: Type): boolean;
resolveName(name: string, location: Node | undefined, meaning: SymbolFlags, excludeGlobals: boolean): Symbol | undefined;
getTypePredicateOfSignature(signature: Signature): TypePredicate | undefined;
filterType(type: Type, f: (t: Type) => boolean): Type;
/**
* Depending on the operation performed, it may be appropriate to throw away the checker
* if the cancellation token is triggered. Typically, if it is used for error checking
Expand Down
24 changes: 24 additions & 0 deletions tests/cases/fourslash/completionsPropertiesWithPromiseUnionType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/// <reference path="fourslash.ts" />
// @strict: true

//// type MyType = {
//// foo: string;
//// };

//// function fakeTest(cb: () => MyType | Promise<MyType>) {}

//// fakeTest(() => {
//// return {
//// /*a*/
//// };
//// });


verify.completions(
{
marker: ['a'],
exact: [
{ name: 'foo', kind: 'property' },
]
}
);