diff --git a/src/services/goToDefinition.ts b/src/services/goToDefinition.ts index 7282903299f..3688a688c53 100644 --- a/src/services/goToDefinition.ts +++ b/src/services/goToDefinition.ts @@ -31,6 +31,7 @@ import { getModeForUsageLocation, getNameFromPropertyName, getNameOfDeclaration, + getObjectFlags, getPropertySymbolsFromContextualType, getTargetLabel, getTextOfPropertyName, @@ -67,14 +68,18 @@ import { isPropertyName, isRightSideOfPropertyAccess, isStaticModifier, + isTypeAliasDeclaration, + isTypeReferenceNode, isVariableDeclaration, last, map, mapDefined, + MappedType, ModifierFlags, moveRangePastModifiers, Node, NodeFlags, + ObjectFlags, Program, resolvePath, ScriptElementKind, @@ -95,6 +100,7 @@ import { Type, TypeChecker, TypeFlags, + TypeReference, unescapeLeadingUnderscores, } from "./_namespaces/ts"; @@ -346,6 +352,72 @@ export function getReferenceAtPosition(sourceFile: SourceFile, position: number, return undefined; } +const typesWithUnwrappedTypeArguments = new Set([ + "Array", + "ArrayLike", + "ReadonlyArray", + "Promise", + "PromiseLike", + "Iterable", + "IterableIterator", + "AsyncIterable", + "Set", + "WeakSet", + "ReadonlySet", + "Map", + "WeakMap", + "ReadonlyMap", + "Partial", + "Required", + "Readonly", + "Pick", + "Omit", +]); + +function shouldUnwrapFirstTypeArgumentTypeDefinitionFromTypeReference(typeChecker: TypeChecker, type: TypeReference): boolean { + const referenceName = type.symbol.name; + if (!typesWithUnwrappedTypeArguments.has(referenceName)) { + return false; + } + const globalType = typeChecker.resolveName(referenceName, /*location*/ undefined, SymbolFlags.Type, /*excludeGlobals*/ false); + return !!globalType && globalType === type.target.symbol; +} + +function shouldUnwrapFirstTypeArgumentTypeDefinitionFromAlias(typeChecker: TypeChecker, type: Type): boolean { + if (!type.aliasSymbol) { + return false; + } + const referenceName = type.aliasSymbol.name; + if (!typesWithUnwrappedTypeArguments.has(referenceName)) { + return false; + } + const globalType = typeChecker.resolveName(referenceName, /*location*/ undefined, SymbolFlags.Type, /*excludeGlobals*/ false); + return !!globalType && globalType === type.aliasSymbol; +} + +function getFirstTypeArgumentDefinitions(typeChecker: TypeChecker, type: Type, node: Node, failedAliasResolution: boolean | undefined): readonly DefinitionInfo[] { + if (!!(getObjectFlags(type) & ObjectFlags.Reference) && shouldUnwrapFirstTypeArgumentTypeDefinitionFromTypeReference(typeChecker, type as TypeReference)) { + return definitionFromType(typeChecker.getTypeArguments(type as TypeReference)[0], typeChecker, node, failedAliasResolution); + } + if (shouldUnwrapFirstTypeArgumentTypeDefinitionFromAlias(typeChecker, type) && type.aliasTypeArguments) { + return definitionFromType(type.aliasTypeArguments[0], typeChecker, node, failedAliasResolution); + } + + if ( + (getObjectFlags(type) & ObjectFlags.Mapped) && + (type as MappedType).target && + shouldUnwrapFirstTypeArgumentTypeDefinitionFromAlias(typeChecker, (type as MappedType).target!) + ) { + const declaration = type.aliasSymbol?.declarations?.[0]; + + if (declaration && isTypeAliasDeclaration(declaration) && isTypeReferenceNode(declaration.type) && declaration.type.typeArguments) { + return definitionFromType(typeChecker.getTypeAtLocation(declaration.type.typeArguments[0]), typeChecker, node, failedAliasResolution); + } + } + + return []; +} + /// Goto type /** @internal */ export function getTypeDefinitionAtPosition(typeChecker: TypeChecker, sourceFile: SourceFile, position: number): readonly DefinitionInfo[] | undefined { @@ -357,16 +429,19 @@ export function getTypeDefinitionAtPosition(typeChecker: TypeChecker, sourceFile if (isImportMeta(node.parent) && node.parent.name === node) { return definitionFromType(typeChecker.getTypeAtLocation(node.parent), typeChecker, node.parent, /*failedAliasResolution*/ false); } - const { symbol, failedAliasResolution } = getSymbol(node, typeChecker, /*stopAtAlias*/ false); if (!symbol) return undefined; const typeAtLocation = typeChecker.getTypeOfSymbolAtLocation(symbol, node); const returnType = tryGetReturnTypeOfFunction(symbol, typeAtLocation, typeChecker); const fromReturnType = returnType && definitionFromType(returnType, typeChecker, node, failedAliasResolution); + // If a function returns 'void' or some other type with no definition, just return the function definition. - const typeDefinitions = fromReturnType && fromReturnType.length !== 0 ? fromReturnType : definitionFromType(typeAtLocation, typeChecker, node, failedAliasResolution); - return typeDefinitions.length ? typeDefinitions + const [resolvedType, typeDefinitions] = fromReturnType && fromReturnType.length !== 0 ? + [returnType, fromReturnType] : + [typeAtLocation, definitionFromType(typeAtLocation, typeChecker, node, failedAliasResolution)]; + + return typeDefinitions.length ? [...getFirstTypeArgumentDefinitions(typeChecker, resolvedType, node, failedAliasResolution), ...typeDefinitions] : !(symbol.flags & SymbolFlags.Value) && symbol.flags & SymbolFlags.Type ? getDefinitionFromSymbol(typeChecker, skipAlias(symbol, typeChecker), node, failedAliasResolution) : undefined; } diff --git a/tests/baselines/reference/goToTypeDefinition_Pick.baseline.jsonc b/tests/baselines/reference/goToTypeDefinition_Pick.baseline.jsonc new file mode 100644 index 00000000000..b231ade9ecc --- /dev/null +++ b/tests/baselines/reference/goToTypeDefinition_Pick.baseline.jsonc @@ -0,0 +1,95 @@ +// === goToType === +// === /tests/cases/fourslash/goToTypeDefinition_Pick.ts === +// type User = [|{| defId: 0 |}{ id: number; name: string; }|]; +// declare const user: Pick +// /*GOTO TYPE*/user +// +// type PickedUser = Pick +// declare const user2: PickedUser +// user2 + +// === lib.d.ts === +// --- (line: 1588) skipped --- +// /** +// * From T, pick a set of properties whose keys are in the union K +// */ +// type Pick = [|{| defId: 1 |}{ +// [P in K]: T[P]; +// }|]; +// +// /** +// * Construct a type with a set of properties K of type T +// --- (line: 1598) skipped --- + + // === Details === + [ + { + "defId": 0, + "kind": "", + "name": "__type", + "containerName": "", + "isLocal": false, + "isAmbient": false, + "unverified": false, + "failedAliasResolution": false + }, + { + "defId": 1, + "kind": "", + "name": "__type", + "containerName": "", + "isLocal": true, + "isAmbient": true, + "unverified": false, + "failedAliasResolution": false + } + ] + + + +// === goToType === +// === /tests/cases/fourslash/goToTypeDefinition_Pick.ts === +// type User = [|{| defId: 0 |}{ id: number; name: string; }|]; +// declare const user: Pick +// user +// +// type PickedUser = Pick +// declare const user2: PickedUser +// /*GOTO TYPE*/user2 + +// === lib.d.ts === +// --- (line: 1588) skipped --- +// /** +// * From T, pick a set of properties whose keys are in the union K +// */ +// type Pick = [|{| defId: 1 |}{ +// [P in K]: T[P]; +// }|]; +// +// /** +// * Construct a type with a set of properties K of type T +// --- (line: 1598) skipped --- + + // === Details === + [ + { + "defId": 0, + "kind": "", + "name": "__type", + "containerName": "", + "isLocal": false, + "isAmbient": false, + "unverified": false, + "failedAliasResolution": false + }, + { + "defId": 1, + "kind": "", + "name": "__type", + "containerName": "", + "isLocal": true, + "isAmbient": true, + "unverified": false, + "failedAliasResolution": false + } + ] \ No newline at end of file diff --git a/tests/baselines/reference/goToTypeDefinition_arrayType.baseline.jsonc b/tests/baselines/reference/goToTypeDefinition_arrayType.baseline.jsonc new file mode 100644 index 00000000000..305b1401602 --- /dev/null +++ b/tests/baselines/reference/goToTypeDefinition_arrayType.baseline.jsonc @@ -0,0 +1,524 @@ +// === goToType === +// === /tests/cases/fourslash/goToTypeDefinition_arrayType.ts === +// type User = [|{| defId: 0 |}{ name: string }|]; +// declare const users: User[] +// /*GOTO TYPE*/users +// +// type UsersArr = Array +// declare const users2: UsersArr +// --- (line: 7) skipped --- + +// === lib.d.ts === +// --- (line: 1310) skipped --- +// slice(start?: number, end?: number): T[]; +// } +// +// <|interface [|{| defId: 1 |}Array|] { +// /** +// * Gets or sets the length of the array. This is a number one higher than the highest index in the array. +// */ +// length: number; +// /** +// * Returns a string representation of an array. +// */ +// toString(): string; +// /** +// * Returns a string representation of an array. The elements are converted to string using their toLocaleString methods. +// */ +// toLocaleString(): string; +// /** +// * Removes the last element from an array and returns it. +// * If the array is empty, undefined is returned and the array is not modified. +// */ +// pop(): T | undefined; +// /** +// * Appends new elements to the end of an array, and returns the new length of the array. +// * @param items New elements to add to the array. +// */ +// push(...items: T[]): number; +// /** +// * Combines two or more arrays. +// * This method returns a new array without modifying any existing arrays. +// * @param items Additional arrays and/or items to add to the end of the array. +// */ +// concat(...items: ConcatArray[]): T[]; +// /** +// * Combines two or more arrays. +// * This method returns a new array without modifying any existing arrays. +// * @param items Additional arrays and/or items to add to the end of the array. +// */ +// concat(...items: (T | ConcatArray)[]): T[]; +// /** +// * Adds all the elements of an array into a string, separated by the specified separator string. +// * @param separator A string used to separate one element of the array from the next in the resulting string. If omitted, the array elements are separated with a comma. +// */ +// join(separator?: string): string; +// /** +// * Reverses the elements in an array in place. +// * This method mutates the array and returns a reference to the same array. +// */ +// reverse(): T[]; +// /** +// * Removes the first element from an array and returns it. +// * If the array is empty, undefined is returned and the array is not modified. +// */ +// shift(): T | undefined; +// /** +// * Returns a copy of a section of an array. +// * For both start and end, a negative index can be used to indicate an offset from the end of the array. +// * For example, -2 refers to the second to last element of the array. +// * @param start The beginning index of the specified portion of the array. +// * If start is undefined, then the slice begins at index 0. +// * @param end The end index of the specified portion of the array. This is exclusive of the element at the index 'end'. +// * If end is undefined, then the slice extends to the end of the array. +// */ +// slice(start?: number, end?: number): T[]; +// /** +// * Sorts an array in place. +// * This method mutates the array and returns a reference to the same array. +// * @param compareFn Function used to determine the order of the elements. It is expected to return +// * a negative value if the first argument is less than the second argument, zero if they're equal, and a positive +// * value otherwise. If omitted, the elements are sorted in ascending, ASCII character order. +// * ```ts +// * [11,2,22,1].sort((a, b) => a - b) +// * ``` +// */ +// sort(compareFn?: (a: T, b: T) => number): this; +// /** +// * Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements. +// * @param start The zero-based location in the array from which to start removing elements. +// * @param deleteCount The number of elements to remove. +// * @returns An array containing the elements that were deleted. +// */ +// splice(start: number, deleteCount?: number): T[]; +// /** +// * Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements. +// * @param start The zero-based location in the array from which to start removing elements. +// * @param deleteCount The number of elements to remove. +// * @param items Elements to insert into the array in place of the deleted elements. +// * @returns An array containing the elements that were deleted. +// */ +// splice(start: number, deleteCount: number, ...items: T[]): T[]; +// /** +// * Inserts new elements at the start of an array, and returns the new length of the array. +// * @param items Elements to insert at the start of the array. +// */ +// unshift(...items: T[]): number; +// /** +// * Returns the index of the first occurrence of a value in an array, or -1 if it is not present. +// * @param searchElement The value to locate in the array. +// * @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0. +// */ +// indexOf(searchElement: T, fromIndex?: number): number; +// /** +// * Returns the index of the last occurrence of a specified value in an array, or -1 if it is not present. +// * @param searchElement The value to locate in the array. +// * @param fromIndex The array index at which to begin searching backward. If fromIndex is omitted, the search starts at the last index in the array. +// */ +// lastIndexOf(searchElement: T, fromIndex?: number): number; +// /** +// * Determines whether all the members of an array satisfy the specified test. +// * @param predicate A function that accepts up to three arguments. The every method calls +// * the predicate function for each element in the array until the predicate returns a value +// * which is coercible to the Boolean value false, or until the end of the array. +// * @param thisArg An object to which the this keyword can refer in the predicate function. +// * If thisArg is omitted, undefined is used as the this value. +// */ +// every(predicate: (value: T, index: number, array: T[]) => value is S, thisArg?: any): this is S[]; +// /** +// * Determines whether all the members of an array satisfy the specified test. +// * @param predicate A function that accepts up to three arguments. The every method calls +// * the predicate function for each element in the array until the predicate returns a value +// * which is coercible to the Boolean value false, or until the end of the array. +// * @param thisArg An object to which the this keyword can refer in the predicate function. +// * If thisArg is omitted, undefined is used as the this value. +// */ +// every(predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): boolean; +// /** +// * Determines whether the specified callback function returns true for any element of an array. +// * @param predicate A function that accepts up to three arguments. The some method calls +// * the predicate function for each element in the array until the predicate returns a value +// * which is coercible to the Boolean value true, or until the end of the array. +// * @param thisArg An object to which the this keyword can refer in the predicate function. +// * If thisArg is omitted, undefined is used as the this value. +// */ +// some(predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): boolean; +// /** +// * Performs the specified action for each element in an array. +// * @param callbackfn A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array. +// * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value. +// */ +// forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void; +// /** +// * Calls a defined callback function on each element of an array, and returns an array that contains the results. +// * @param callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array. +// * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value. +// */ +// map(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[]; +// /** +// * Returns the elements of an array that meet the condition specified in a callback function. +// * @param predicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array. +// * @param thisArg An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value. +// */ +// filter(predicate: (value: T, index: number, array: T[]) => value is S, thisArg?: any): S[]; +// /** +// * Returns the elements of an array that meet the condition specified in a callback function. +// * @param predicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array. +// * @param thisArg An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value. +// */ +// filter(predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): T[]; +// /** +// * Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. +// * @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array. +// * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. +// */ +// reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T; +// reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue: T): T; +// /** +// * Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. +// * @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array. +// * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. +// */ +// reduce(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U; +// /** +// * Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. +// * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array. +// * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. +// */ +// reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T; +// reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue: T): T; +// /** +// * Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. +// * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array. +// * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. +// */ +// reduceRight(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U; +// +// [n: number]: T; +// }|> +// +// interface ArrayConstructor { +// new(arrayLength?: number): any[]; +// --- (line: 1500) skipped --- + +// --- (line: 1505) skipped --- +// readonly prototype: any[]; +// } +// +// <|declare var [|{| defId: 2 |}Array|]: ArrayConstructor;|> +// +// interface TypedPropertyDescriptor { +// enumerable?: boolean; +// --- (line: 1513) skipped --- + + // === Details === + [ + { + "defId": 0, + "kind": "", + "name": "__type", + "containerName": "", + "isLocal": false, + "isAmbient": false, + "unverified": false, + "failedAliasResolution": false + }, + { + "defId": 1, + "kind": "var", + "name": "Array", + "containerName": "", + "isLocal": false, + "isAmbient": true, + "unverified": false, + "failedAliasResolution": false + }, + { + "defId": 2, + "kind": "var", + "name": "Array", + "containerName": "", + "isLocal": false, + "isAmbient": true, + "unverified": false, + "failedAliasResolution": false + } + ] + + + +// === goToType === +// === /tests/cases/fourslash/goToTypeDefinition_arrayType.ts === +// type User = [|{| defId: 0 |}{ name: string }|]; +// declare const users: User[] +// users +// +// type UsersArr = Array +// declare const users2: UsersArr +// /*GOTO TYPE*/users2 +// +// class CustomArray extends Array { immutableReverse() { return [...this].reverse() } } +// declare const users3: CustomArray +// users3 + +// === lib.d.ts === +// --- (line: 1310) skipped --- +// slice(start?: number, end?: number): T[]; +// } +// +// <|interface [|{| defId: 1 |}Array|] { +// /** +// * Gets or sets the length of the array. This is a number one higher than the highest index in the array. +// */ +// length: number; +// /** +// * Returns a string representation of an array. +// */ +// toString(): string; +// /** +// * Returns a string representation of an array. The elements are converted to string using their toLocaleString methods. +// */ +// toLocaleString(): string; +// /** +// * Removes the last element from an array and returns it. +// * If the array is empty, undefined is returned and the array is not modified. +// */ +// pop(): T | undefined; +// /** +// * Appends new elements to the end of an array, and returns the new length of the array. +// * @param items New elements to add to the array. +// */ +// push(...items: T[]): number; +// /** +// * Combines two or more arrays. +// * This method returns a new array without modifying any existing arrays. +// * @param items Additional arrays and/or items to add to the end of the array. +// */ +// concat(...items: ConcatArray[]): T[]; +// /** +// * Combines two or more arrays. +// * This method returns a new array without modifying any existing arrays. +// * @param items Additional arrays and/or items to add to the end of the array. +// */ +// concat(...items: (T | ConcatArray)[]): T[]; +// /** +// * Adds all the elements of an array into a string, separated by the specified separator string. +// * @param separator A string used to separate one element of the array from the next in the resulting string. If omitted, the array elements are separated with a comma. +// */ +// join(separator?: string): string; +// /** +// * Reverses the elements in an array in place. +// * This method mutates the array and returns a reference to the same array. +// */ +// reverse(): T[]; +// /** +// * Removes the first element from an array and returns it. +// * If the array is empty, undefined is returned and the array is not modified. +// */ +// shift(): T | undefined; +// /** +// * Returns a copy of a section of an array. +// * For both start and end, a negative index can be used to indicate an offset from the end of the array. +// * For example, -2 refers to the second to last element of the array. +// * @param start The beginning index of the specified portion of the array. +// * If start is undefined, then the slice begins at index 0. +// * @param end The end index of the specified portion of the array. This is exclusive of the element at the index 'end'. +// * If end is undefined, then the slice extends to the end of the array. +// */ +// slice(start?: number, end?: number): T[]; +// /** +// * Sorts an array in place. +// * This method mutates the array and returns a reference to the same array. +// * @param compareFn Function used to determine the order of the elements. It is expected to return +// * a negative value if the first argument is less than the second argument, zero if they're equal, and a positive +// * value otherwise. If omitted, the elements are sorted in ascending, ASCII character order. +// * ```ts +// * [11,2,22,1].sort((a, b) => a - b) +// * ``` +// */ +// sort(compareFn?: (a: T, b: T) => number): this; +// /** +// * Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements. +// * @param start The zero-based location in the array from which to start removing elements. +// * @param deleteCount The number of elements to remove. +// * @returns An array containing the elements that were deleted. +// */ +// splice(start: number, deleteCount?: number): T[]; +// /** +// * Removes elements from an array and, if necessary, inserts new elements in their place, returning the deleted elements. +// * @param start The zero-based location in the array from which to start removing elements. +// * @param deleteCount The number of elements to remove. +// * @param items Elements to insert into the array in place of the deleted elements. +// * @returns An array containing the elements that were deleted. +// */ +// splice(start: number, deleteCount: number, ...items: T[]): T[]; +// /** +// * Inserts new elements at the start of an array, and returns the new length of the array. +// * @param items Elements to insert at the start of the array. +// */ +// unshift(...items: T[]): number; +// /** +// * Returns the index of the first occurrence of a value in an array, or -1 if it is not present. +// * @param searchElement The value to locate in the array. +// * @param fromIndex The array index at which to begin the search. If fromIndex is omitted, the search starts at index 0. +// */ +// indexOf(searchElement: T, fromIndex?: number): number; +// /** +// * Returns the index of the last occurrence of a specified value in an array, or -1 if it is not present. +// * @param searchElement The value to locate in the array. +// * @param fromIndex The array index at which to begin searching backward. If fromIndex is omitted, the search starts at the last index in the array. +// */ +// lastIndexOf(searchElement: T, fromIndex?: number): number; +// /** +// * Determines whether all the members of an array satisfy the specified test. +// * @param predicate A function that accepts up to three arguments. The every method calls +// * the predicate function for each element in the array until the predicate returns a value +// * which is coercible to the Boolean value false, or until the end of the array. +// * @param thisArg An object to which the this keyword can refer in the predicate function. +// * If thisArg is omitted, undefined is used as the this value. +// */ +// every(predicate: (value: T, index: number, array: T[]) => value is S, thisArg?: any): this is S[]; +// /** +// * Determines whether all the members of an array satisfy the specified test. +// * @param predicate A function that accepts up to three arguments. The every method calls +// * the predicate function for each element in the array until the predicate returns a value +// * which is coercible to the Boolean value false, or until the end of the array. +// * @param thisArg An object to which the this keyword can refer in the predicate function. +// * If thisArg is omitted, undefined is used as the this value. +// */ +// every(predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): boolean; +// /** +// * Determines whether the specified callback function returns true for any element of an array. +// * @param predicate A function that accepts up to three arguments. The some method calls +// * the predicate function for each element in the array until the predicate returns a value +// * which is coercible to the Boolean value true, or until the end of the array. +// * @param thisArg An object to which the this keyword can refer in the predicate function. +// * If thisArg is omitted, undefined is used as the this value. +// */ +// some(predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): boolean; +// /** +// * Performs the specified action for each element in an array. +// * @param callbackfn A function that accepts up to three arguments. forEach calls the callbackfn function one time for each element in the array. +// * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value. +// */ +// forEach(callbackfn: (value: T, index: number, array: T[]) => void, thisArg?: any): void; +// /** +// * Calls a defined callback function on each element of an array, and returns an array that contains the results. +// * @param callbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array. +// * @param thisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value. +// */ +// map(callbackfn: (value: T, index: number, array: T[]) => U, thisArg?: any): U[]; +// /** +// * Returns the elements of an array that meet the condition specified in a callback function. +// * @param predicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array. +// * @param thisArg An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value. +// */ +// filter(predicate: (value: T, index: number, array: T[]) => value is S, thisArg?: any): S[]; +// /** +// * Returns the elements of an array that meet the condition specified in a callback function. +// * @param predicate A function that accepts up to three arguments. The filter method calls the predicate function one time for each element in the array. +// * @param thisArg An object to which the this keyword can refer in the predicate function. If thisArg is omitted, undefined is used as the this value. +// */ +// filter(predicate: (value: T, index: number, array: T[]) => unknown, thisArg?: any): T[]; +// /** +// * Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. +// * @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array. +// * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. +// */ +// reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T; +// reduce(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue: T): T; +// /** +// * Calls the specified callback function for all the elements in an array. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. +// * @param callbackfn A function that accepts up to four arguments. The reduce method calls the callbackfn function one time for each element in the array. +// * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. +// */ +// reduce(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U; +// /** +// * Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. +// * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array. +// * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. +// */ +// reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T): T; +// reduceRight(callbackfn: (previousValue: T, currentValue: T, currentIndex: number, array: T[]) => T, initialValue: T): T; +// /** +// * Calls the specified callback function for all the elements in an array, in descending order. The return value of the callback function is the accumulated result, and is provided as an argument in the next call to the callback function. +// * @param callbackfn A function that accepts up to four arguments. The reduceRight method calls the callbackfn function one time for each element in the array. +// * @param initialValue If initialValue is specified, it is used as the initial value to start the accumulation. The first call to the callbackfn function provides this value as an argument instead of an array value. +// */ +// reduceRight(callbackfn: (previousValue: U, currentValue: T, currentIndex: number, array: T[]) => U, initialValue: U): U; +// +// [n: number]: T; +// }|> +// +// interface ArrayConstructor { +// new(arrayLength?: number): any[]; +// --- (line: 1500) skipped --- + +// --- (line: 1505) skipped --- +// readonly prototype: any[]; +// } +// +// <|declare var [|{| defId: 2 |}Array|]: ArrayConstructor;|> +// +// interface TypedPropertyDescriptor { +// enumerable?: boolean; +// --- (line: 1513) skipped --- + + // === Details === + [ + { + "defId": 0, + "kind": "", + "name": "__type", + "containerName": "", + "isLocal": false, + "isAmbient": false, + "unverified": false, + "failedAliasResolution": false + }, + { + "defId": 1, + "kind": "var", + "name": "Array", + "containerName": "", + "isLocal": false, + "isAmbient": true, + "unverified": false, + "failedAliasResolution": false + }, + { + "defId": 2, + "kind": "var", + "name": "Array", + "containerName": "", + "isLocal": false, + "isAmbient": true, + "unverified": false, + "failedAliasResolution": false + } + ] + + + +// === goToType === +// === /tests/cases/fourslash/goToTypeDefinition_arrayType.ts === +// --- (line: 5) skipped --- +// declare const users2: UsersArr +// users2 +// +// <|class [|CustomArray|] extends Array { immutableReverse() { return [...this].reverse() } }|> +// declare const users3: CustomArray +// /*GOTO TYPE*/users3 + + // === Details === + [ + { + "kind": "class", + "name": "CustomArray", + "containerName": "", + "isLocal": false, + "isAmbient": false, + "unverified": false, + "failedAliasResolution": false + } + ] \ No newline at end of file diff --git a/tests/baselines/reference/goToTypeDefinition_promiseType.baseline.jsonc b/tests/baselines/reference/goToTypeDefinition_promiseType.baseline.jsonc new file mode 100644 index 00000000000..5af203d472a --- /dev/null +++ b/tests/baselines/reference/goToTypeDefinition_promiseType.baseline.jsonc @@ -0,0 +1,119 @@ +// === goToType === +// === /tests/cases/fourslash/goToTypeDefinition_promiseType.ts === +// type User = [|{| defId: 0 |}{ name: string }|]; +// async function /*GOTO TYPE*/getUser() { return { name: "Bob" } satisfies User as User } +// +// const promisedBob = getUser() +// +// export {} + +// === lib.d.ts === +// --- (line: 1531) skipped --- +// /** +// * Represents the completion of an asynchronous operation +// */ +// <|interface [|{| defId: 1 |}Promise|] { +// /** +// * Attaches callbacks for the resolution and/or rejection of the Promise. +// * @param onfulfilled The callback to execute when the Promise is resolved. +// * @param onrejected The callback to execute when the Promise is rejected. +// * @returns A Promise for the completion of which ever callback is executed. +// */ +// then(onfulfilled?: ((value: T) => TResult1 | PromiseLike) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike) | undefined | null): Promise; +// +// /** +// * Attaches a callback for only the rejection of the Promise. +// * @param onrejected The callback to execute when the Promise is rejected. +// * @returns A Promise for the completion of the callback. +// */ +// catch(onrejected?: ((reason: any) => TResult | PromiseLike) | undefined | null): Promise; +// }|> +// +// /** +// * Recursively unwraps the "awaited type" of a type. Non-promise "thenables" should resolve to `never`. This emulates the behavior of `await`. +// --- (line: 1554) skipped --- + + // === Details === + [ + { + "defId": 0, + "kind": "", + "name": "__type", + "containerName": "", + "isLocal": true, + "isAmbient": false, + "unverified": false, + "failedAliasResolution": false + }, + { + "defId": 1, + "kind": "interface", + "name": "Promise", + "containerName": "", + "isLocal": false, + "isAmbient": true, + "unverified": false, + "failedAliasResolution": false + } + ] + + + +// === goToType === +// === /tests/cases/fourslash/goToTypeDefinition_promiseType.ts === +// type User = [|{| defId: 0 |}{ name: string }|]; +// async function getUser() { return { name: "Bob" } satisfies User as User } +// +// const /*GOTO TYPE*/promisedBob = getUser() +// +// export {} + +// === lib.d.ts === +// --- (line: 1531) skipped --- +// /** +// * Represents the completion of an asynchronous operation +// */ +// <|interface [|{| defId: 1 |}Promise|] { +// /** +// * Attaches callbacks for the resolution and/or rejection of the Promise. +// * @param onfulfilled The callback to execute when the Promise is resolved. +// * @param onrejected The callback to execute when the Promise is rejected. +// * @returns A Promise for the completion of which ever callback is executed. +// */ +// then(onfulfilled?: ((value: T) => TResult1 | PromiseLike) | undefined | null, onrejected?: ((reason: any) => TResult2 | PromiseLike) | undefined | null): Promise; +// +// /** +// * Attaches a callback for only the rejection of the Promise. +// * @param onrejected The callback to execute when the Promise is rejected. +// * @returns A Promise for the completion of the callback. +// */ +// catch(onrejected?: ((reason: any) => TResult | PromiseLike) | undefined | null): Promise; +// }|> +// +// /** +// * Recursively unwraps the "awaited type" of a type. Non-promise "thenables" should resolve to `never`. This emulates the behavior of `await`. +// --- (line: 1554) skipped --- + + // === Details === + [ + { + "defId": 0, + "kind": "", + "name": "__type", + "containerName": "", + "isLocal": true, + "isAmbient": false, + "unverified": false, + "failedAliasResolution": false + }, + { + "defId": 1, + "kind": "interface", + "name": "Promise", + "containerName": "", + "isLocal": false, + "isAmbient": true, + "unverified": false, + "failedAliasResolution": false + } + ] \ No newline at end of file diff --git a/tests/baselines/reference/goToTypeDefinition_typeReference.baseline.jsonc b/tests/baselines/reference/goToTypeDefinition_typeReference.baseline.jsonc new file mode 100644 index 00000000000..47a4ce7a115 --- /dev/null +++ b/tests/baselines/reference/goToTypeDefinition_typeReference.baseline.jsonc @@ -0,0 +1,19 @@ +// === goToType === +// === /tests/cases/fourslash/goToTypeDefinition_typeReference.ts === +// type User = { name: string }; +// type Box = [|{ value: T }|]; +// declare const boxedUser: Box +// /*GOTO TYPE*/boxedUser + + // === Details === + [ + { + "kind": "", + "name": "__type", + "containerName": "", + "isLocal": false, + "isAmbient": false, + "unverified": false, + "failedAliasResolution": false + } + ] \ No newline at end of file diff --git a/tests/cases/fourslash/goToTypeDefinition_Pick.ts b/tests/cases/fourslash/goToTypeDefinition_Pick.ts new file mode 100644 index 00000000000..5c61f3becaa --- /dev/null +++ b/tests/cases/fourslash/goToTypeDefinition_Pick.ts @@ -0,0 +1,11 @@ +/// + +//// type User = { id: number; name: string; }; +//// declare const user: Pick +//// /*reference*/user +//// +//// type PickedUser = Pick +//// declare const user2: PickedUser +//// /*reference2*/user2 + +verify.baselineGoToType("reference", "reference2"); diff --git a/tests/cases/fourslash/goToTypeDefinition_arrayType.ts b/tests/cases/fourslash/goToTypeDefinition_arrayType.ts new file mode 100644 index 00000000000..5de1f2f9bc2 --- /dev/null +++ b/tests/cases/fourslash/goToTypeDefinition_arrayType.ts @@ -0,0 +1,15 @@ +/// + +//// type User = { name: string }; +//// declare const users: User[] +//// /*reference*/users +//// +//// type UsersArr = Array +//// declare const users2: UsersArr +//// /*reference2*/users2 +//// +//// class CustomArray extends Array { immutableReverse() { return [...this].reverse() } } +//// declare const users3: CustomArray +//// /*reference3*/users3 + +verify.baselineGoToType("reference", "reference2", "reference3"); diff --git a/tests/cases/fourslash/goToTypeDefinition_promiseType.ts b/tests/cases/fourslash/goToTypeDefinition_promiseType.ts new file mode 100644 index 00000000000..bc8fe43c07a --- /dev/null +++ b/tests/cases/fourslash/goToTypeDefinition_promiseType.ts @@ -0,0 +1,10 @@ +/// + +//// type User = { name: string }; +//// async function /*reference*/getUser() { return { name: "Bob" } satisfies User as User } +//// +//// const /*reference2*/promisedBob = getUser() +//// +//// export {} + +verify.baselineGoToType("reference", "reference2"); diff --git a/tests/cases/fourslash/goToTypeDefinition_typeReference.ts b/tests/cases/fourslash/goToTypeDefinition_typeReference.ts new file mode 100644 index 00000000000..a7964727a21 --- /dev/null +++ b/tests/cases/fourslash/goToTypeDefinition_typeReference.ts @@ -0,0 +1,8 @@ +/// + +//// type User = { name: string }; +//// type Box = { value: T }; +//// declare const boxedUser: Box +//// /*reference*/boxedUser + +verify.baselineGoToType("reference");