@@ -325,6 +325,7 @@ import {
325
325
getModeForUsageLocation,
326
326
getModifiers,
327
327
getModuleInstanceState,
328
+ getNameFromImportAttribute,
328
329
getNameFromIndexInfo,
329
330
getNameOfDeclaration,
330
331
getNameOfExpando,
@@ -407,6 +408,7 @@ import {
407
408
IdentifierTypePredicate,
408
409
idText,
409
410
IfStatement,
411
+ ImportAttribute,
410
412
ImportAttributes,
411
413
ImportCall,
412
414
ImportClause,
@@ -553,6 +555,7 @@ import {
553
555
isIdentifierTypePredicate,
554
556
isIdentifierTypeReference,
555
557
isIfStatement,
558
+ isImportAttributes,
556
559
isImportCall,
557
560
isImportClause,
558
561
isImportDeclaration,
@@ -2179,6 +2182,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2179
2182
var deferredGlobalImportMetaType: ObjectType;
2180
2183
var deferredGlobalImportMetaExpressionType: ObjectType;
2181
2184
var deferredGlobalImportCallOptionsType: ObjectType | undefined;
2185
+ var deferredGlobalImportAttributesType: ObjectType | undefined;
2182
2186
var deferredGlobalDisposableType: ObjectType | undefined;
2183
2187
var deferredGlobalAsyncDisposableType: ObjectType | undefined;
2184
2188
var deferredGlobalExtractSymbol: Symbol | undefined;
@@ -11555,6 +11559,25 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
11555
11559
return widenTypeForVariableLikeDeclaration(getTypeForVariableLikeDeclaration(declaration, /*includeOptionality*/ true, CheckMode.Normal), declaration, reportErrors);
11556
11560
}
11557
11561
11562
+ function getTypeFromImportAttributes(node: ImportAttributes): Type {
11563
+ const links = getNodeLinks(node);
11564
+ if (!links.resolvedType) {
11565
+ const symbol = createSymbol(SymbolFlags.ObjectLiteral, InternalSymbolName.ImportAttributes);
11566
+ const members = createSymbolTable();
11567
+ forEach(node.elements, attr => {
11568
+ const member = createSymbol(SymbolFlags.Property, getNameFromImportAttribute(attr));
11569
+ member.parent = symbol;
11570
+ member.links.type = checkImportAttribute(attr);
11571
+ member.links.target = member;
11572
+ members.set(member.escapedName, member);
11573
+ });
11574
+ const type = createAnonymousType(symbol, members, emptyArray, emptyArray, emptyArray);
11575
+ type.objectFlags |= ObjectFlags.ObjectLiteral | ObjectFlags.NonInferrableType;
11576
+ links.resolvedType = type;
11577
+ }
11578
+ return links.resolvedType;
11579
+ }
11580
+
11558
11581
function isGlobalSymbolConstructor(node: Node) {
11559
11582
const symbol = getSymbolOfNode(node);
11560
11583
const globalSymbol = getGlobalESSymbolConstructorTypeSymbol(/*reportErrors*/ false);
@@ -16417,6 +16440,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
16417
16440
return (deferredGlobalImportCallOptionsType ||= getGlobalType("ImportCallOptions" as __String, /*arity*/ 0, reportErrors)) || emptyObjectType;
16418
16441
}
16419
16442
16443
+ function getGlobalImportAttributesType(reportErrors: boolean) {
16444
+ return (deferredGlobalImportAttributesType ||= getGlobalType("ImportAttributes" as __String, /*arity*/ 0, reportErrors)) || emptyObjectType;
16445
+ }
16446
+
16420
16447
function getGlobalESSymbolConstructorSymbol(reportErrors: boolean): Symbol | undefined {
16421
16448
return deferredGlobalESSymbolConstructorSymbol ||= getGlobalValueSymbol("Symbol" as __String, reportErrors);
16422
16449
}
@@ -30904,6 +30931,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
30904
30931
case SyntaxKind.JsxOpeningElement:
30905
30932
case SyntaxKind.JsxSelfClosingElement:
30906
30933
return getContextualJsxElementAttributesType(parent as JsxOpeningLikeElement, contextFlags);
30934
+ case SyntaxKind.ImportAttribute:
30935
+ return getContextualImportAttributeType(parent as ImportAttribute);
30907
30936
}
30908
30937
return undefined;
30909
30938
}
@@ -30950,6 +30979,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
30950
30979
}
30951
30980
}
30952
30981
30982
+ function getContextualImportAttributeType(node: ImportAttribute) {
30983
+ return getTypeOfPropertyOfContextualType(getGlobalImportAttributesType(/*reportErrors*/ false), getNameFromImportAttribute(node));
30984
+ }
30985
+
30953
30986
function getContextualJsxElementAttributesType(node: JsxOpeningLikeElement, contextFlags: ContextFlags | undefined) {
30954
30987
if (isJsxOpeningElement(node) && contextFlags !== ContextFlags.Completions) {
30955
30988
const index = findContextualNode(node.parent, /*includeCaches*/ !contextFlags);
@@ -45991,6 +46024,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
45991
46024
function checkImportAttributes(declaration: ImportDeclaration | ExportDeclaration) {
45992
46025
const node = declaration.attributes;
45993
46026
if (node) {
46027
+ const importAttributesType = getGlobalImportAttributesType(/*reportErrors*/ true);
46028
+ if (importAttributesType !== emptyObjectType) {
46029
+ checkTypeAssignableTo(getTypeFromImportAttributes(node), getNullableType(importAttributesType, TypeFlags.Undefined), node);
46030
+ }
46031
+
45994
46032
const validForTypeAttributes = isExclusivelyTypeOnlyImportOrExport(declaration);
45995
46033
const override = getResolutionModeOverride(node, validForTypeAttributes ? grammarErrorOnNode : undefined);
45996
46034
const isImportAttributes = declaration.attributes.token === SyntaxKind.WithKeyword;
@@ -46020,6 +46058,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
46020
46058
}
46021
46059
}
46022
46060
46061
+ function checkImportAttribute(node: ImportAttribute) {
46062
+ return getRegularTypeOfLiteralType(checkExpressionCached(node.value));
46063
+ }
46064
+
46023
46065
function checkImportDeclaration(node: ImportDeclaration) {
46024
46066
if (checkGrammarModuleElementContext(node, isInJSFile(node) ? Diagnostics.An_import_declaration_can_only_be_used_at_the_top_level_of_a_module : Diagnostics.An_import_declaration_can_only_be_used_at_the_top_level_of_a_namespace_or_module)) {
46025
46067
// If we hit an import declaration in an illegal context, just bail out to avoid cascading errors.
@@ -47700,6 +47742,10 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
47700
47742
return checkMetaPropertyKeyword(node.parent);
47701
47743
}
47702
47744
47745
+ if (isImportAttributes(node)) {
47746
+ return getGlobalImportAttributesType(/*reportErrors*/ false);
47747
+ }
47748
+
47703
47749
return errorType;
47704
47750
}
47705
47751
0 commit comments