8000 `in` operator typeguard can widen types by marekdedic · Pull Request #46403 · microsoft/TypeScript · GitHub
[go: up one dir, main page]

Skip to content

in operator typeguard can widen types #46403

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

Closed
wants to merge 86 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
5a41201
not escape unicode char for import path string.
ShuiRuTian Jul 7, 2020
775fd9c
Merge remote-tracking branch 'upstream/master'
ShuiRuTian Jul 7, 2020
b974add
beta version.
ShuiRuTian Jul 24, 2020
49b574b
beta version.
ShuiRuTian Jul 25, 2020
734f4c2
fix.
ShuiRuTian Jul 25, 2020
b6cbb3e
revert code for debug.
ShuiRuTian Jul 25, 2020
d6cabee
fix some condition.
ShuiRuTian Jul 26, 2020
48b27e7
clearer code.
ShuiRuTian Jul 28, 2020
5936e27
fix
ShuiRuTian Jul 28, 2020
02ff40a
fix
ShuiRuTian Jul 28, 2020
6ec0edc
Merge branch 'master' into in-operator-add-property
ShuiRuTian Sep 4, 2020
79e81be
immutatble.
ShuiRuTian Sep 4, 2020
c2fc19e
revert some strange format change.
ShuiRuTian Sep 4, 2020
a5d17f3
correct test baseline.
ShuiRuTian Sep 4, 2020
540545a
Merge branch 'master' into in-operator-add-property
ShuiRuTian Sep 9, 2020
b6f36cd
fix test baseline
ShuiRuTian Sep 9, 2020
13b276c
fix lint.
ShuiRuTian Sep 9, 2020
84f5eeb
more clean code.
ShuiRuTian Oct 28, 2020
f2ee389
Merge branch 'main' into in-operator-add-property
marekdedic Oct 16, 2021
758d8df
Updated in keyword type widening to reflect changes in master
marekdedic Oct 16, 2021
d10b8d3
Grammar fix (widden -> widen)
marekdedic Oct 16, 2021
e245b88
Accepted baselines with in keyword type widening
marekdedic Oct 17, 2021
c79a1fd
Accepted comments into baseline
marekdedic Oct 17, 2021
f8586b8
Lint fixes
marekdedic Oct 17, 2021
3e70f10
More sophisticated tests for in operator type widening
marekdedic Oct 19, 2021
5def93a
Added more (failing) test cases for in typeguard type widening
marekdedic Oct 19, 2021
26a982b
Modified TS2361 to allow unknown on right-hand side
marekdedic Oct 19, 2021
e93b2dc
Revert "Modified TS2361 to allow unknown on right-hand side"
marekdedic Oct 21, 2021
7d49f33
Working less ambitious use of in operator widening
marekdedic Oct 21, 2021
728a8a3
Added tests for in operator type widening in complex control flows
marekdedic Oct 21, 2021
47d206e
Added tests for in operator type widening in yet more complex control…
marekdedic Oct 21, 2021
a29c5a5
WidenedByNarrow -> WidenedByIn
marekdedic Oct 21, 2021
5bcc2e6
createWidenType -> createWidenedType
marekdedic Oct 21, 2021
78a50e9
Updated intersection type code to better match master
marekdedic Oct 21, 2021
f7bcee0
isSomeDirectSubtypeContainsPropName -> someDirectSubtypeContainsPropName
marekdedic Oct 21, 2021
2015750
Simplified intersection type widening by widening first anonymous type
marekdedic Oct 21, 2021
08dcd94
Deduplicated code from someDirectSubtypeContainsPropName and merged i…
marekdedic Oct 22, 2021
9da464b
Fixed commit 08dcd94
marekdedic Oct 22, 2021
94b9311
Split out widenObjectType function
marekdedic Oct 22, 2021
a47d9b2
In operator injecting properties directly into object types
marekdedic Oct 22, 2021
5594882
Only adding properties directly to anonymous object types
marekdedic Oct 22, 2021
b9f6aaf
Accepted test baselines for test where property order changed
marekdedic Oct 22, 2021
4b4e63b
Reorganized and simplified widenTypeWithSymbol()
marekdedic Oct 22, 2021
fbd0454
Added more in operator tests
marekdedic Oct 22, 2021
6b7b0b1
Removed dead code
marekdedic Oct 22, 2021
225f5a1
Simplified controlFlowInOperator unit tests
marekdedic Oct 22, 2021
1f5c6c5
Reorganized and documented the logic of narrowOrWidenTypeByInKeyword
marekdedic Oct 23, 2021
c7641f1
Revert #38610 as it is no longer needed
marekdedic Oct 23, 2021
704ae12
Accepted tests baselines
marekdedic Oct 24, 2021
83fda77
In operator not touching globalThis
marekdedic Oct 24, 2021
d739a5a
Split filterUnionOrIntersectionType from filterType and deduplicated it
marekdedic Oct 24, 2021
b75b129
Split out typeTests
marekdedic Oct 24, 2021
624f586
Simplified logic around globalThis for in operator
marekdedic Oct 24, 2021
e53fb8a
Marked type tests as internal
marekdedic Oct 24, 2021
8bc9ef4
Added in operator unit test with call signature
marekdedic Oct 24, 2021
5eb7dcd
More thorough tests
marekdedic Oct 24, 2021
6010fff
Added in operator unit test with index signature
8000 marekdedic Oct 24, 2021
caab233
Accepted test baseline
marekdedic Oct 24, 2021
762e56b
Simplified object flag checking, thanks @jakebailey
marekdedic Jan 14, 2022
b16a4a1
Reverted instersectionTypes back to Map<string, Type>
marekdedic Jan 14, 2022
ae755aa
Merge branch 'master' into in-operator-add-property
marekdedic Mar 6, 2022
b1fadcd
Update package-lock.json
typescript-bot Mar 12, 2022
46e7bc6
Update package-lock.json
typescript-bot Mar 14, 2022
efdcc0d
Update package-lock.json
typescript-bot Mar 22, 2022
0bbe651
Update package-lock.json
typescript-bot Mar 24, 2022
58e9420
Merge branch 'main' of github.com:marekdedic/TypeScript
marekdedic Mar 26, 2022
aca5344
Merge branch 'main' into in-operator-add-property
marekdedic Mar 26, 2022
9c681b6
reverted quickInfoImportNonunicodePath test changes
marekdedic Mar 26, 2022
acf47c8
Removed dead code
marekdedic Mar 26, 2022
7293a62
Removed the need for includePartialProperties
marekdedic Mar 26, 2022
8c3c2ac
Dissolved function isUnionOrIntersectionType
marekdedic Mar 26, 2022
dfe51a4
Dissolved function isUnionType
marekdedic Mar 26, 2022
5c32123
Dissolved function isIntersectionType
marekdedic Mar 26, 2022
d4be592
Dissolved function isObjectType
marekdedic Mar 26, 2022
2f997b5
Update package-lock.json
typescript-bot Apr 12, 2022
ed9eea3
Update package-lock.json
typescript-bot Apr 14, 2022
02c8b2d
Merge branch 'main' of https://github.com/microsoft/TypeScript
marekdedic Apr 14, 2022
75dc33e
Merge branch 'main' of github.com:marekdedic/TypeScript
marekdedic Apr 14, 2022
1b82d01
Update package-lock.json
typescript-bot Apr 15, 2022
f5555df
Update package-lock.json
typescript-bot Apr 19, 2022
ca14f27
Update package-lock.json
typescript-bot Apr 22, 2022
13d2150
Update package-lock.json
typescript-bot Apr 25, 2022
c891690
Update package-lock.json
typescript-bot Apr 26, 2022
2b60aa8
Merge branch 'main' of https://github.com/microsoft/TypeScript
marekdedic Apr 26, 2022
30f89ae
Merge branch 'main' of github.com:marekdedic/TypeScript
marekdedic Apr 26, 2022
ec41cbb
Merge branch 'main' into in-operator-add-property
marekdedic Apr 26, 2022
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
Prev Previous commit
Next Next commit
beta version.
  • Loading branch information
ShuiRuTian committed Jul 25, 2020
commit 49b574bf2476e95e3fbadf1e86a2072aa5cd398c
71 changes: 56 additions & 15 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21459,30 +21459,71 @@ namespace ts {
return !assumeTrue;
}

function narrowByInKeyword(type: Type, literal: LiteralExpression, assumeTrue: boolean) {
function narrowOrWiddenTypeByInKeyword(type: Type, literal: LiteralExpression, assumeTrue: boolean) {
const propName = escapeLeadingUnderscores(literal.text);
const addSymbol = createSymbol(SymbolFlags.Property, propName);
addSymbol.type = unknownType;

if (type.flags & (TypeFlags.Union | TypeFlags.Object) || isThisTypeParameter(type)) {
if ((type.flags & (TypeFlags.Union | TypeFlags.Object) || isThisTypeParameter(type)) && isSomeDirectSubtypeContainsPropName(type, propName)) {
return filterType(type, t => isTypePresencePossible(t, propName, assumeTrue));
}
else{
if (isSomeDirectSubtypeContainsPropName(type, propName)) {
if (type.flags !== TypeFlags.Intersection) {
const newObjType = createObjectType(ObjectFlags.Anonymous);
const newSymbolObject = createSymbol(SymbolFlags.Property, propName, 0);
newObjType.properties = [];
newObjType.properties.push(newSymbolObject);
else if (assumeTrue && !isSomeDirectSubtypeContainsPropName(type, propName)) {
// if type is intersection, we might have added type into it, and we just need to add into this type again rather than a new one.
// else add a new anonymous object type which contains the type and widden the origional type with it.
if (type.flags & TypeFlags.Intersection) {
// try to get the first Anonymous Object type to add new type to it.
const firstAnonymousObjectType: Type | undefined = (type as UnionOrIntersectionType).types.find(t => (t.flags & TypeFlags.Object) && (<ObjectType>t).objectFlags & ObjectFlags.Anonymous);
if (firstAnonymousObjectType) {
const members = createSymbolTable();
members.set(propName, addSymbol);
if ((firstAnonymousObjectType as ObjectType).members) {
mergeSymbolTable(members, (firstAnonymousObjectType as ObjectType).members!);
}
(firstAnonymousObjectType as ObjectType).members = members;
(firstAnonymousObjectType as ObjectType).properties = getNamedMembers(members);
}
else {
const members = createSymbolTable();
members.set(propName, addSymbol);
const newObjType = createAnonymousType(undefined, members, emptyArray, emptyArray, undefined, undefined);
return createIntersectionType([type, newObjType]);
}
}
// if type is intersection, get the first Object type to add new index to it.
// else add a new object type and make the type become an intersection type.
// Add a 8000 string index to the type.
else {
const members = createSymbolTable();
members.set(propName, addSymbol);
const newObjType = createAnonymousType(undefined, members, emptyArray, emptyArray, undefined, undefined);
return createIntersectionType([type, newObjType]);
}
}
return type;

function isSomeDirectSubtypeContainsPropName(type: Type, propName: __String) {
function isSomeDirectSubtypeContainsPropName(type1: Type, name: __String) {
// could not assume type is union or intersection, like (A|B)&C, C is added string object, this is intersection now, but we need judge it like Union.
const prop = getPropertyOfType(type, propName);

let prop;
const type = getReducedApparentType(type1);
if (type.flags & TypeFlags.Object) {
const resolved = resolveStructuredTypeMembers(<ObjectType>type);
const symbol = resolved.members.get(name);
if (symbol && symbolIsValue(symbol)) {
prop = symbol;
}
const functionType = resolved === anyFunctionType ? globalFunctionType :
resolved.callSignatures.length ? globalCallableFunctionType :
resolved.constructSignatures.length ? globalNewableFunctionType :
undefined;
if (functionType) {
const symbol = getPropertyOfObjectType(functionType, name);
if (symbol) {
prop= symbol;
}
}
return getPropertyOfObjectType(globalObjectType, name);
}
if (type.flags & TypeFlags.UnionOrIntersection) {
prop = getUnionOrIntersectionProperty(<UnionOrIntersectionType>type, name);
}
if (prop) {
return true;
}
Expand Down Expand Up @@ -21543,7 +21584,7 @@ namespace ts {
case SyntaxKind.InKeyword:
const target = getReferenceCandidate(expr.right);
if (isStringLiteralLike(expr.left) && isMatchingReference(reference, target)) {
return narrowByInKeyword(type, expr.left, assumeTrue);
return narrowOrWiddenTypeByInKeyword(type, expr.left, assumeTrue);
}
break;
case SyntaxKind.CommaToken:
Expand Down
0