Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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 @@ -12881,6 +12881,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);
Copy link
Member

Choose a reason for hiding this comment

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

This seems like more work than the rest of the function does, so I'm worried about performance problems on mapped types with a nameType. I'll run the perf tests, but I don't know that any such mapped types are in there. @weswigham do you have an idea if this isTypeAssignableTo call is likely to be (relatively) slow here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note that it's the same check that got used here:
https://github.com/microsoft/TypeScript/pull/48699/files#diff-d9ab6589e714c71e657f601cf30ff51dfc607fc98419bf72e04f6b0fa92cc4b8R15745

That said - that other PR calls this just from within getSimplifiedIndexedAccessType and this is in the resolveMappedTypeMembers so the impact on the perf could still be different as it depends on how frequent are calls to those caller functions

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 @@ -12927,9 +12928,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
prop.keyType = keyType;
if (modifiersProp) {
prop.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