Skip to content

Filtering mapped types declarations #51650

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 7 commits into from
Feb 16, 2023
Merged
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
5 changes: 2 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13042,6 +13042,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const typeParameter = getTypeParameterFromMappedType(type);
const constraintType = getConstraintTypeFromMappedType(type);
const nameType = getNameTypeFromMappedType(type.target as MappedType || type);
const isFilteringMappedType = nameType && isTypeAssignableTo(nameType, typeParameter);
const templateType = getTemplateTypeFromMappedType(type.target as MappedType || type);
const modifiersType = getApparentType(getModifiersTypeFromMappedType(type)); // The 'T' in 'keyof T'
const templateModifiers = getMappedTypeModifiers(type);
Expand Down Expand Up @@ -13088,9 +13089,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
prop.links.keyType = keyType;
if (modifiersProp) {
prop.links.syntheticOrigin = modifiersProp;
// If the mapped type has an `as XXX` clause, the property name likely won't match the declaration name and
// multiple properties may map to the same name. Thus, we attach no declarations to the symbol.
prop.declarations = nameType ? undefined : modifiersProp.declarations;
prop.declarations = !nameType || isFilteringMappedType ? modifiersProp.declarations : undefined;
}
members.set(propName, prop);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,284 @@
// === /tests/cases/fourslash/findAllReferencesFilteringMappedTypeProperty.ts ===
// const obj = { /*FIND ALL REFS*/[|a|]: 1, b: 2 };
// const filtered: { [P in keyof typeof obj as P extends 'b' ? never : P]: 0; } = { [|a|]: 0 };
// filtered.[|a|];

[
{
"definition": {
"containerKind": "",
"containerName": "",
"fileName": "/tests/cases/fourslash/findAllReferencesFilteringMappedTypeProperty.ts",
"kind": "property",
"name": "(property) a: number",
"textSpan": {
"start": 14,
"length": 1
},
"displayParts": [
{
"text": "(",
"kind": "punctuation"
},
{
"text": "property",
"kind": "text"
},
{
"text": ")",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "a",
"kind": "propertyName"
},
{
"text": ":",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "number",
"kind": "keyword"
}
],
"contextSpan": {
"start": 14,
"length": 4
}
},
"references": [
{
"textSpan": {
"start": 14,
"length": 1
},
"fileName": "/tests/cases/fourslash/findAllReferencesFilteringMappedTypeProperty.ts",
"contextSpan": {
"start": 14,
"length": 4
},
"isWriteAccess": true,
"isDefinition": true
},
{
"textSpan": {
"start": 109,
"length": 1
},
"fileName": "/tests/cases/fourslash/findAllReferencesFilteringMappedTypeProperty.ts",
"contextSpan": {
"start": 109,
"length": 4
},
"isWriteAccess": true,
"isDefinition": false
},
{
"textSpan": {
"start": 126,
"length": 1
},
"fileName": "/tests/cases/fourslash/findAllReferencesFilteringMappedTypeProperty.ts",
"isWriteAccess": false,
"isDefinition": false
}
]
}
]

// === /tests/cases/fourslash/findAllReferencesFilteringMappedTypeProperty.ts ===
// const obj = { [|a|]: 1, b: 2 };
// const filtered: { [P in keyof typeof obj as P extends 'b' ? never : P]: 0; } = { /*FIND ALL REFS*/[|a|]: 0 };
// filtered.[|a|];

[
{
"definition": {
"containerKind": "",
"containerName": "",
"fileName": "/tests/cases/fourslash/findAllReferencesFilteringMappedTypeProperty.ts",
"kind": "property",
"name": "(property) a: number",
"textSpan": {
"start": 14,
"length": 1
},
"displayParts": [
{
"text": "(",
"kind": "punctuation"
},
{
"text": "property",
"kind": "text"
},
{
"text": ")",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "a",
"kind": "propertyName"
},
{
"text": ":",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "number",
"kind": "keyword"
}
],
"contextSpan": {
"start": 14,
"length": 4
}
},
"references": [
{
"textSpan": {
"start": 14,
"length": 1
},
"fileName": "/tests/cases/fourslash/findAllReferencesFilteringMappedTypeProperty.ts",
"contextSpan": {
"start": 14,
"length": 4
},
"isWriteAccess": true,
"isDefinition": false
},
{
"textSpan": {
"start": 109,
"length": 1
},
"fileName": "/tests/cases/fourslash/findAllReferencesFilteringMappedTypeProperty.ts",
"contextSpan": {
"start": 109,
"length": 4
},
"isWriteAccess": true,
"isDefinition": true
},
{
"textSpan": {
"start": 126,
"length": 1
},
"fileName": "/tests/cases/fourslash/findAllReferencesFilteringMappedTypeProperty.ts",
"isWriteAccess": false,
"isDefinition": false
}
]
}
]

// === /tests/cases/fourslash/findAllReferencesFilteringMappedTypeProperty.ts ===
// const obj = { [|a|]: 1, b: 2 };
// const filtered: { [P in keyof typeof obj as P extends 'b' ? never : P]: 0; } = { [|a|]: 0 };
// filtered./*FIND ALL REFS*/[|a|];

[
{
"definition": {
"containerKind": "",
"containerName": "",
"fileName": "/tests/cases/fourslash/findAllReferencesFilteringMappedTypeProperty.ts",
"kind": "property",
"name": "(property) a: number",
"textSpan": {
"start": 14,
"length": 1
},
"displayParts": [
{
"text": "(",
"kind": "punctuation"
},
{
"text": "property",
"kind": "text"
},
{
"text": ")",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "a",
"kind": "propertyName"
},
{
"text": ":",
"kind": "punctuation"
},
{
"text": " ",
"kind": "space"
},
{
"text": "number",
"kind": "keyword"
}
],
"contextSpan": {
"start": 14,
"length": 4
}
},
"references": [
{
"textSpan": {
"start": 14,
"length": 1
},
"fileName": "/tests/cases/fourslash/findAllReferencesFilteringMappedTypeProperty.ts",
"contextSpan": {
"start": 14,
"length": 4
},
"isWriteAccess": true
},
{
"textSpan": {
"start": 109,
"length": 1
},
"fileName": "/tests/cases/fourslash/findAllReferencesFilteringMappedTypeProperty.ts",
"contextSpan": {
"start": 109,
"length": 4
},
"isWriteAccess": true
},
{
"textSpan": {
"start": 126,
"length": 1
},
"fileName": "/tests/cases/fourslash/findAllReferencesFilteringMappedTypeProperty.ts",
"isWriteAccess": false
}
]
}
]
Loading