Create synthetic default exports for dynamic imports#17492
Conversation
| memberTable.set(InternalSymbolName.Default, newSymbol); | ||
| const anonymousSymbol = createSymbol(SymbolFlags.TypeLiteral, InternalSymbolName.Type); | ||
| const defaultContainingObject = createAnonymousType(anonymousSymbol, memberTable, emptyArray, emptyArray, /*stringIndexInfo*/ undefined, /*numberIndexInfo*/ undefined); | ||
| const resolved = resolveStructuredTypeMembers(defaultContainingObject); |
There was a problem hiding this comment.
resolveStructuredTypeMembers is a no-op since createAnonymousType sets the members propery.
| const anonymousSymbol = createSymbol(SymbolFlags.TypeLiteral, InternalSymbolName.Type); | ||
| const defaultContainingObject = createAnonymousType(anonymousSymbol, memberTable, emptyArray, emptyArray, /*stringIndexInfo*/ undefined, /*numberIndexInfo*/ undefined); | ||
| const resolved = resolveStructuredTypeMembers(defaultContainingObject); | ||
| resolved.properties = [newSymbol]; |
There was a problem hiding this comment.
This shouldn't be necessary since createAnonymousType sets properties for you.
| if (!defaultSymbol) { | ||
| const memberTable = createSymbolTable(); | ||
| const newSymbol = createSymbol(SymbolFlags.Alias, InternalSymbolName.Default); | ||
| getSymbolLinks(newSymbol).target = resolveSymbol(symbol); |
There was a problem hiding this comment.
The getSymbolLinks call is unnecessary here. newSymbol is already a TransientSymbol which is its own SymbolLinks.
| const defaultContainingObject = createAnonymousType(anonymousSymbol, memberTable, emptyArray, emptyArray, /*stringIndexInfo*/ undefined, /*numberIndexInfo*/ undefined); | ||
| const resolved = resolveStructuredTypeMembers(defaultContainingObject); | ||
| resolved.properties = [newSymbol]; | ||
| getSymbolLinks(anonymousSymbol).type = defaultContainingObject; |
There was a problem hiding this comment.
As with newSymbol above, anonymousSymbol is a TransientSymbol which is its own SymbolLinks.
| if (allowSyntheticDefaultImports && type && type !== unknownType) { | ||
| const synthType = type as SyntheticDefaultModuleType; | ||
| if (!synthType.syntheticType) { | ||
| const defaultSymbol = getPropertyOfType(type, InternalSymbolName.Default); |
There was a problem hiding this comment.
defaultSymbol is only used once, consider inlining this into the if condition.
| const resolved = resolveStructuredTypeMembers(defaultContainingObject); | ||
| resolved.properties = [newSymbol]; | ||
| getSymbolLinks(anonymousSymbol).type = defaultContainingObject; | ||
| const newType = getIntersectionType([type, defaultContainingObject]); |
There was a problem hiding this comment.
newType is only used once, consider inlining the variable.
0230d36 to
7c72c4f
Compare
|
@rbuckton done. |
rbuckton
left a comment
There was a problem hiding this comment.
One minor comment, then it looks good to merge.
| return createPromiseReturnType(node, anyType); | ||
| } | ||
|
|
||
| function syntheticDefaultType(type: Type, symbol: Symbol): Type { |
There was a problem hiding this comment.
Can you use a more descriptive name like getTypeWithSyntheticDefaultImport? At a very high level this behaves similarly to getTypeWithThisArgument and getTypeWithDefault and as such should share a similar nomenclature.
Fixes #17444
If synthetic default imports is on, we now intersect an anonymous object with a default member equal to the module with the module type itself iff there is no module default member already.