mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-05-30 01:04:49 -05:00
Merge pull request #29510 from Microsoft/constContexts
Const contexts for literal expressions
This commit is contained in:
@@ -3789,7 +3789,7 @@ namespace ts {
|
||||
context.approximateLength += (symbolName(propertySymbol).length + 1);
|
||||
context.enclosingDeclaration = saveEnclosingDeclaration;
|
||||
const optionalToken = propertySymbol.flags & SymbolFlags.Optional ? createToken(SyntaxKind.QuestionToken) : undefined;
|
||||
if (propertySymbol.flags & (SymbolFlags.Function | SymbolFlags.Method) && !getPropertiesOfObjectType(propertyType).length) {
|
||||
if (propertySymbol.flags & (SymbolFlags.Function | SymbolFlags.Method) && !getPropertiesOfObjectType(propertyType).length && !isReadonlySymbol(propertySymbol)) {
|
||||
const signatures = getSignaturesOfType(propertyType, SignatureKind.Call);
|
||||
for (const signature of signatures) {
|
||||
const methodDeclaration = <MethodSignature>signatureToSignatureDeclarationHelper(signature, SyntaxKind.MethodSignature, context);
|
||||
@@ -4791,7 +4791,7 @@ namespace ts {
|
||||
if (!isTypeAssignableTo(getLiteralTypeFromProperty(prop, TypeFlags.StringOrNumberLiteralOrUnique), omitKeyType)
|
||||
&& !(getDeclarationModifierFlagsFromSymbol(prop) & (ModifierFlags.Private | ModifierFlags.Protected))
|
||||
&& isSpreadableProperty(prop)) {
|
||||
members.set(prop.escapedName, getSpreadSymbol(prop));
|
||||
members.set(prop.escapedName, getSpreadSymbol(prop, /*readonly*/ false));
|
||||
}
|
||||
}
|
||||
const stringIndexInfo = getIndexInfoOfType(source, IndexKind.String);
|
||||
@@ -9124,7 +9124,7 @@ namespace ts {
|
||||
function createTupleType(elementTypes: ReadonlyArray<Type>, minLength = elementTypes.length, hasRestElement = false, readonly = false, associatedNames?: __String[]) {
|
||||
const arity = elementTypes.length;
|
||||
if (arity === 1 && hasRestElement) {
|
||||
return createArrayType(elementTypes[0]);
|
||||
return createArrayType(elementTypes[0], readonly);
|
||||
}
|
||||
const tupleType = getTupleTypeOfArity(arity, minLength, arity > 0 && hasRestElement, readonly, associatedNames);
|
||||
return elementTypes.length ? createTypeReference(tupleType, elementTypes) : tupleType;
|
||||
@@ -10258,7 +10258,7 @@ namespace ts {
|
||||
* this function should be called in a left folding style, with left = previous result of getSpreadType
|
||||
* and right = the new element to be spread.
|
||||
*/
|
||||
function getSpreadType(left: Type, right: Type, symbol: Symbol | undefined, typeFlags: TypeFlags, objectFlags: ObjectFlags): Type {
|
||||
function getSpreadType(left: Type, right: Type, symbol: Symbol | undefined, typeFlags: TypeFlags, objectFlags: ObjectFlags, readonly: boolean): Type {
|
||||
if (left.flags & TypeFlags.Any || right.flags & TypeFlags.Any) {
|
||||
return anyType;
|
||||
}
|
||||
@@ -10272,10 +10272,10 @@ namespace ts {
|
||||
return left;
|
||||
}
|
||||
if (left.flags & TypeFlags.Union) {
|
||||
return mapType(left, t => getSpreadType(t, right, symbol, typeFlags, objectFlags));
|
||||
return mapType(left, t => getSpreadType(t, right, symbol, typeFlags, objectFlags, readonly));
|
||||
}
|
||||
if (right.flags & TypeFlags.Union) {
|
||||
return mapType(right, t => getSpreadType(left, t, symbol, typeFlags, objectFlags));
|
||||
return mapType(right, t => getSpreadType(left, t, symbol, typeFlags, objectFlags, readonly));
|
||||
}
|
||||
if (right.flags & (TypeFlags.BooleanLike | TypeFlags.NumberLike | TypeFlags.BigIntLike | TypeFlags.StringLike | TypeFlags.EnumLike | TypeFlags.NonPrimitive | TypeFlags.Index)) {
|
||||
return left;
|
||||
@@ -10292,7 +10292,7 @@ namespace ts {
|
||||
const types = (<IntersectionType>left).types;
|
||||
const lastLeft = types[types.length - 1];
|
||||
if (isNonGenericObjectType(lastLeft) && isNonGenericObjectType(right)) {
|
||||
return getIntersectionType(concatenate(types.slice(0, types.length - 1), [getSpreadType(lastLeft, right, symbol, typeFlags, objectFlags)]));
|
||||
return getIntersectionType(concatenate(types.slice(0, types.length - 1), [getSpreadType(lastLeft, right, symbol, typeFlags, objectFlags, readonly)]));
|
||||
}
|
||||
}
|
||||
return getIntersectionType([left, right]);
|
||||
@@ -10317,7 +10317,7 @@ namespace ts {
|
||||
skippedPrivateMembers.set(rightProp.escapedName, true);
|
||||
}
|
||||
else if (isSpreadableProperty(rightProp)) {
|
||||
members.set(rightProp.escapedName, getSpreadSymbol(rightProp));
|
||||
members.set(rightProp.escapedName, getSpreadSymbol(rightProp, readonly));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10341,7 +10341,7 @@ namespace ts {
|
||||
}
|
||||
}
|
||||
else {
|
||||
members.set(leftProp.escapedName, getSpreadSymbol(leftProp));
|
||||
members.set(leftProp.escapedName, getSpreadSymbol(leftProp, readonly));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10350,8 +10350,8 @@ namespace ts {
|
||||
members,
|
||||
emptyArray,
|
||||
emptyArray,
|
||||
getNonReadonlyIndexSignature(stringIndexInfo),
|
||||
getNonReadonlyIndexSignature(numberIndexInfo));
|
||||
getIndexInfoWithReadonly(stringIndexInfo, readonly),
|
||||
getIndexInfoWithReadonly(numberIndexInfo, readonly));
|
||||
spread.flags |= TypeFlags.ContainsObjectLiteral | typeFlags;
|
||||
spread.objectFlags |= ObjectFlags.ObjectLiteral | ObjectFlags.ContainsSpread | objectFlags;
|
||||
return spread;
|
||||
@@ -10363,14 +10363,13 @@ namespace ts {
|
||||
!prop.declarations.some(decl => isClassLike(decl.parent));
|
||||
}
|
||||
|
||||
function getSpreadSymbol(prop: Symbol) {
|
||||
const isReadonly = isReadonlySymbol(prop);
|
||||
function getSpreadSymbol(prop: Symbol, readonly: boolean) {
|
||||
const isSetonlyAccessor = prop.flags & SymbolFlags.SetAccessor && !(prop.flags & SymbolFlags.GetAccessor);
|
||||
if (!isReadonly && !isSetonlyAccessor) {
|
||||
if (!isSetonlyAccessor && readonly === isReadonlySymbol(prop)) {
|
||||
return prop;
|
||||
}
|
||||
const flags = SymbolFlags.Property | (prop.flags & SymbolFlags.Optional);
|
||||
const result = createSymbol(flags, prop.escapedName);
|
||||
const result = createSymbol(flags, prop.escapedName, readonly ? CheckFlags.Readonly : 0);
|
||||
result.type = isSetonlyAccessor ? undefinedType : getTypeOfSymbol(prop);
|
||||
result.declarations = prop.declarations;
|
||||
result.nameType = prop.nameType;
|
||||
@@ -10378,11 +10377,8 @@ namespace ts {
|
||||
return result;
|
||||
}
|
||||
|
||||
function getNonReadonlyIndexSignature(index: IndexInfo | undefined) {
|
||||
if (index && index.isReadonly) {
|
||||
return createIndexInfo(index.type, /*isReadonly*/ false, index.declaration);
|
||||
}
|
||||
return index;
|
||||
function getIndexInfoWithReadonly(info: IndexInfo | undefined, readonly: boolean) {
|
||||
return info && info.isReadonly !== readonly ? createIndexInfo(info.type, readonly, info.declaration) : info;
|
||||
}
|
||||
|
||||
function createLiteralType(flags: TypeFlags, value: string | number | PseudoBigInt, symbol: Symbol | undefined) {
|
||||
@@ -17809,7 +17805,7 @@ namespace ts {
|
||||
return getContextualTypeForArgument(<CallExpression | NewExpression>parent, node);
|
||||
case SyntaxKind.TypeAssertionExpression:
|
||||
case SyntaxKind.AsExpression:
|
||||
return getTypeFromTypeNode((<AssertionExpression>parent).type);
|
||||
return isConstTypeReference((<AssertionExpression>parent).type) ? undefined : getTypeFromTypeNode((<AssertionExpression>parent).type);
|
||||
case SyntaxKind.BinaryExpression:
|
||||
return getContextualTypeForBinaryOperand(node);
|
||||
case SyntaxKind.PropertyAssignment:
|
||||
@@ -18095,6 +18091,7 @@ namespace ts {
|
||||
const elementTypes: Type[] = [];
|
||||
const inDestructuringPattern = isAssignmentTarget(node);
|
||||
const contextualType = getApparentTypeOfContextualType(node);
|
||||
const inConstContext = isConstContext(node);
|
||||
for (let index = 0; index < elementCount; index++) {
|
||||
const e = elements[index];
|
||||
if (inDestructuringPattern && e.kind === SyntaxKind.SpreadElement) {
|
||||
@@ -18137,7 +18134,7 @@ namespace ts {
|
||||
type.pattern = node;
|
||||
return type;
|
||||
}
|
||||
else if (tupleResult = getArrayLiteralTupleTypeIfApplicable(elementTypes, contextualType, hasRestElement, elementCount)) {
|
||||
else if (tupleResult = getArrayLiteralTupleTypeIfApplicable(elementTypes, contextualType, hasRestElement, elementCount, inConstContext)) {
|
||||
return tupleResult;
|
||||
}
|
||||
else if (forceTuple) {
|
||||
@@ -18146,14 +18143,14 @@ namespace ts {
|
||||
}
|
||||
return createArrayType(elementTypes.length ?
|
||||
getUnionType(elementTypes, UnionReduction.Subtype) :
|
||||
strictNullChecks ? implicitNeverType : undefinedWideningType);
|
||||
strictNullChecks ? implicitNeverType : undefinedWideningType, inConstContext);
|
||||
}
|
||||
|
||||
function getArrayLiteralTupleTypeIfApplicable(elementTypes: Type[], contextualType: Type | undefined, hasRestElement: boolean, elementCount = elementTypes.length) {
|
||||
function getArrayLiteralTupleTypeIfApplicable(elementTypes: Type[], contextualType: Type | undefined, hasRestElement: boolean, elementCount = elementTypes.length, readonly = false) {
|
||||
// Infer a tuple type when the contextual type is or contains a tuple-like type
|
||||
if (contextualType && forEachType(contextualType, isTupleLikeType)) {
|
||||
if (readonly || (contextualType && forEachType(contextualType, isTupleLikeType))) {
|
||||
const minLength = elementCount - (hasRestElement ? 1 : 0);
|
||||
const pattern = contextualType.pattern;
|
||||
const pattern = contextualType && contextualType.pattern;
|
||||
// If array literal is contextually typed by a binding pattern or an assignment pattern, pad the resulting
|
||||
// tuple type with the corresponding binding or assignment element types to make the lengths equal.
|
||||
if (!hasRestElement && pattern && (pattern.kind === SyntaxKind.ArrayBindingPattern || pattern.kind === SyntaxKind.ArrayLiteralExpression)) {
|
||||
@@ -18171,7 +18168,7 @@ namespace ts {
|
||||
}
|
||||
}
|
||||
}
|
||||
return createTupleType(elementTypes, minLength, hasRestElement);
|
||||
return createTupleType(elementTypes, minLength, hasRestElement, readonly);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18243,15 +18240,15 @@ namespace ts {
|
||||
return links.resolvedType;
|
||||
}
|
||||
|
||||
function getObjectLiteralIndexInfo(propertyNodes: NodeArray<ObjectLiteralElementLike>, offset: number, properties: Symbol[], kind: IndexKind): IndexInfo {
|
||||
function getObjectLiteralIndexInfo(node: ObjectLiteralExpression, offset: number, properties: Symbol[], kind: IndexKind): IndexInfo {
|
||||
const propTypes: Type[] = [];
|
||||
for (let i = 0; i < properties.length; i++) {
|
||||
if (kind === IndexKind.String || isNumericName(propertyNodes[i + offset].name!)) {
|
||||
if (kind === IndexKind.String || isNumericName(node.properties[i + offset].name!)) {
|
||||
propTypes.push(getTypeOfSymbol(properties[i]));
|
||||
}
|
||||
}
|
||||
const unionType = propTypes.length ? getUnionType(propTypes, UnionReduction.Subtype) : undefinedType;
|
||||
return createIndexInfo(unionType, /*isReadonly*/ false);
|
||||
return createIndexInfo(unionType, isConstContext(node));
|
||||
}
|
||||
|
||||
function getImmediateAliasedSymbol(symbol: Symbol): Symbol | undefined {
|
||||
@@ -18279,6 +18276,8 @@ namespace ts {
|
||||
const contextualType = getApparentTypeOfContextualType(node);
|
||||
const contextualTypeHasPattern = contextualType && contextualType.pattern &&
|
||||
(contextualType.pattern.kind === SyntaxKind.ObjectBindingPattern || contextualType.pattern.kind === SyntaxKind.ObjectLiteralExpression);
|
||||
const inConstContext = isConstContext(node);
|
||||
const checkFlags = inConstContext ? CheckFlags.Readonly : 0;
|
||||
const isInJavascript = isInJSFile(node) && !isInJsonFile(node);
|
||||
const enumTag = getJSDocEnumTag(node);
|
||||
const isJSObjectLiteral = !contextualType && isInJavascript && !enumTag;
|
||||
@@ -18313,8 +18312,8 @@ namespace ts {
|
||||
typeFlags |= type.flags;
|
||||
const nameType = computedNameType && isTypeUsableAsPropertyName(computedNameType) ? computedNameType : undefined;
|
||||
const prop = nameType ?
|
||||
createSymbol(SymbolFlags.Property | member.flags, getPropertyNameFromType(nameType), CheckFlags.Late) :
|
||||
createSymbol(SymbolFlags.Property | member.flags, member.escapedName);
|
||||
createSymbol(SymbolFlags.Property | member.flags, getPropertyNameFromType(nameType), checkFlags | CheckFlags.Late) :
|
||||
createSymbol(SymbolFlags.Property | member.flags, member.escapedName, checkFlags);
|
||||
if (nameType) {
|
||||
prop.nameType = nameType;
|
||||
}
|
||||
@@ -18358,7 +18357,7 @@ namespace ts {
|
||||
checkExternalEmitHelpers(memberDecl, ExternalEmitHelpers.Assign);
|
||||
}
|
||||
if (propertiesArray.length > 0) {
|
||||
spread = getSpreadType(spread, createObjectLiteralType(), node.symbol, propagatedFlags, ObjectFlags.FreshLiteral);
|
||||
spread = getSpreadType(spread, createObjectLiteralType(), node.symbol, propagatedFlags, ObjectFlags.FreshLiteral, inConstContext);
|
||||
propertiesArray = [];
|
||||
propertiesTable = createSymbolTable();
|
||||
hasComputedStringProperty = false;
|
||||
@@ -18370,7 +18369,7 @@ namespace ts {
|
||||
error(memberDecl, Diagnostics.Spread_types_may_only_be_created_from_object_types);
|
||||
return errorType;
|
||||
}
|
||||
spread = getSpreadType(spread, type, node.symbol, propagatedFlags, ObjectFlags.FreshLiteral);
|
||||
spread = getSpreadType(spread, type, node.symbol, propagatedFlags, ObjectFlags.FreshLiteral, inConstContext);
|
||||
offset = i + 1;
|
||||
continue;
|
||||
}
|
||||
@@ -18420,7 +18419,7 @@ namespace ts {
|
||||
|
||||
if (spread !== emptyObjectType) {
|
||||
if (propertiesArray.length > 0) {
|
||||
spread = getSpreadType(spread, createObjectLiteralType(), node.symbol, propagatedFlags, ObjectFlags.FreshLiteral);
|
||||
spread = getSpreadType(spread, createObjectLiteralType(), node.symbol, propagatedFlags, ObjectFlags.FreshLiteral, inConstContext);
|
||||
}
|
||||
return spread;
|
||||
}
|
||||
@@ -18428,8 +18427,8 @@ namespace ts {
|
||||
return createObjectLiteralType();
|
||||
|
||||
function createObjectLiteralType() {
|
||||
const stringIndexInfo = hasComputedStringProperty ? getObjectLiteralIndexInfo(node.properties, offset, propertiesArray, IndexKind.String) : undefined;
|
||||
const numberIndexInfo = hasComputedNumberProperty ? getObjectLiteralIndexInfo(node.properties, offset, propertiesArray, IndexKind.Number) : undefined;
|
||||
const stringIndexInfo = hasComputedStringProperty ? getObjectLiteralIndexInfo(node, offset, propertiesArray, IndexKind.String) : undefined;
|
||||
const numberIndexInfo = hasComputedNumberProperty ? getObjectLiteralIndexInfo(node, offset, propertiesArray, IndexKind.Number) : undefined;
|
||||
const result = createAnonymousType(node.symbol, propertiesTable, emptyArray, emptyArray, stringIndexInfo, numberIndexInfo);
|
||||
result.flags |= TypeFlags.ContainsObjectLiteral | typeFlags & TypeFlags.PropagatingFlags;
|
||||
result.objectFlags |= ObjectFlags.ObjectLiteral | freshObjectLiteralFlag;
|
||||
@@ -18559,7 +18558,7 @@ namespace ts {
|
||||
else {
|
||||
Debug.assert(attributeDecl.kind === SyntaxKind.JsxSpreadAttribute);
|
||||
if (attributesTable.size > 0) {
|
||||
spread = getSpreadType(spread, createJsxAttributesType(), attributes.symbol, typeFlags, objectFlags);
|
||||
spread = getSpreadType(spread, createJsxAttributesType(), attributes.symbol, typeFlags, objectFlags, /*readonly*/ false);
|
||||
attributesTable = createSymbolTable();
|
||||
}
|
||||
const exprType = checkExpressionCached(attributeDecl.expression, checkMode);
|
||||
@@ -18567,7 +18566,7 @@ namespace ts {
|
||||
hasSpreadAnyType = true;
|
||||
}
|
||||
if (isValidSpreadType(exprType)) {
|
||||
spread = getSpreadType(spread, exprType, attributes.symbol, typeFlags, objectFlags);
|
||||
spread = getSpreadType(spread, exprType, attributes.symbol, typeFlags, objectFlags, /*readonly*/ false);
|
||||
}
|
||||
else {
|
||||
typeToIntersect = typeToIntersect ? getIntersectionType([typeToIntersect, exprType]) : exprType;
|
||||
@@ -18577,7 +18576,7 @@ namespace ts {
|
||||
|
||||
if (!hasSpreadAnyType) {
|
||||
if (attributesTable.size > 0) {
|
||||
spread = getSpreadType(spread, createJsxAttributesType(), attributes.symbol, typeFlags, objectFlags);
|
||||
spread = getSpreadType(spread, createJsxAttributesType(), attributes.symbol, typeFlags, objectFlags, /*readonly*/ false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18609,7 +18608,7 @@ namespace ts {
|
||||
const childPropMap = createSymbolTable();
|
||||
childPropMap.set(jsxChildrenPropertyName, childrenPropSymbol);
|
||||
spread = getSpreadType(spread, createAnonymousType(attributes.symbol, childPropMap, emptyArray, emptyArray, /*stringIndexInfo*/ undefined, /*numberIndexInfo*/ undefined),
|
||||
attributes.symbol, typeFlags, objectFlags);
|
||||
attributes.symbol, typeFlags, objectFlags, /*readonly*/ false);
|
||||
|
||||
}
|
||||
}
|
||||
@@ -21281,7 +21280,7 @@ namespace ts {
|
||||
const anonymousSymbol = createSymbol(SymbolFlags.TypeLiteral, InternalSymbolName.Type);
|
||||
const defaultContainingObject = createAnonymousType(anonymousSymbol, memberTable, emptyArray, emptyArray, /*stringIndexInfo*/ undefined, /*numberIndexInfo*/ undefined);
|
||||
anonymousSymbol.type = defaultContainingObject;
|
||||
synthType.syntheticType = isValidSpreadType(type) ? getSpreadType(type, defaultContainingObject, anonymousSymbol, /*typeFLags*/ 0, /*objectFlags*/ 0) : defaultContainingObject;
|
||||
synthType.syntheticType = isValidSpreadType(type) ? getSpreadType(type, defaultContainingObject, anonymousSymbol, /*typeFLags*/ 0, /*objectFlags*/ 0, /*readonly*/ false) : defaultContainingObject;
|
||||
}
|
||||
else {
|
||||
synthType.syntheticType = type;
|
||||
@@ -21333,12 +21332,39 @@ namespace ts {
|
||||
return checkAssertionWorker(node, node.type, node.expression);
|
||||
}
|
||||
|
||||
function isValidConstAssertionArgument(node: Node): boolean {
|
||||
switch (node.kind) {
|
||||
case SyntaxKind.StringLiteral:
|
||||
case SyntaxKind.NoSubstitutionTemplateLiteral:
|
||||
case SyntaxKind.NumericLiteral:
|
||||
case SyntaxKind.BigIntLiteral:
|
||||
case SyntaxKind.TrueKeyword:
|
||||
case SyntaxKind.FalseKeyword:
|
||||
case SyntaxKind.ArrayLiteralExpression:
|
||||
case SyntaxKind.ObjectLiteralExpression:
|
||||
return true;
|
||||
case SyntaxKind.ParenthesizedExpression:
|
||||
return isValidConstAssertionArgument((<ParenthesizedExpression>node).expression);
|
||||
case SyntaxKind.PrefixUnaryExpression:
|
||||
const op = (<PrefixUnaryExpression>node).operator;
|
||||
const arg = (<PrefixUnaryExpression>node).operand;
|
||||
return op === SyntaxKind.MinusToken && (arg.kind === SyntaxKind.NumericLiteral || arg.kind === SyntaxKind.BigIntLiteral) ||
|
||||
op === SyntaxKind.PlusToken && arg.kind === SyntaxKind.NumericLiteral;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
function checkAssertionWorker(errNode: Node, type: TypeNode, expression: UnaryExpression | Expression, checkMode?: CheckMode) {
|
||||
const exprType = getRegularTypeOfObjectLiteral(getBaseTypeOfLiteralType(checkExpression(expression, checkMode)));
|
||||
|
||||
let exprType = checkExpression(expression, checkMode);
|
||||
if (isConstTypeReference(type)) {
|
||||
if (!isValidConstAssertionArgument(expression)) {
|
||||
error(expression, Diagnostics.A_const_assertion_can_only_be_applied_to_a_string_number_boolean_array_or_object_literal);
|
||||
}
|
||||
return getRegularTypeOfLiteralType(exprType);
|
||||
}
|
||||
checkSourceElement(type);
|
||||
exprType = getRegularTypeOfObjectLiteral(getBaseTypeOfLiteralType(exprType));
|
||||
const targetType = getTypeFromTypeNode(type);
|
||||
|
||||
if (produceDiagnostics && targetType !== errorType) {
|
||||
const widenedType = getWidenedType(exprType);
|
||||
if (!isTypeComparableTo(targetType, widenedType)) {
|
||||
@@ -23058,12 +23084,20 @@ namespace ts {
|
||||
return false;
|
||||
}
|
||||
|
||||
function isConstContext(node: Expression): boolean {
|
||||
const parent = node.parent;
|
||||
return isAssertionExpression(parent) && isConstTypeReference(parent.type) ||
|
||||
(isParenthesizedExpression(parent) || isArrayLiteralExpression(parent) || isSpreadElement(parent)) && isConstContext(parent) ||
|
||||
(isPropertyAssignment(parent) || isShorthandPropertyAssignment(parent)) && isConstContext(parent.parent);
|
||||
}
|
||||
|
||||
function checkExpressionForMutableLocation(node: Expression, checkMode: CheckMode | undefined, contextualType?: Type, forceTuple?: boolean): Type {
|
||||
if (arguments.length === 2) {
|
||||
contextualType = getContextualType(node);
|
||||
}
|
||||
const type = checkExpression(node, checkMode, forceTuple);
|
||||
return isTypeAssertion(node) ? type :
|
||||
return isConstContext(node) ? getRegularTypeOfLiteralType(type) :
|
||||
isTypeAssertion(node) ? type :
|
||||
getWidenedLiteralLikeTypeForContextualType(type, contextualType);
|
||||
}
|
||||
|
||||
@@ -23127,7 +23161,7 @@ namespace ts {
|
||||
return getReturnTypeOfSignature(signature);
|
||||
}
|
||||
}
|
||||
else if (expr.kind === SyntaxKind.TypeAssertionExpression || expr.kind === SyntaxKind.AsExpression) {
|
||||
else if (isAssertionExpression(expr) && !isConstTypeReference(expr.type)) {
|
||||
return getTypeFromTypeNode((<TypeAssertion>expr).type);
|
||||
}
|
||||
// Otherwise simply call checkExpression. Ideally, the entire family of checkXXX functions
|
||||
|
||||
@@ -1027,6 +1027,10 @@
|
||||
"category": "Error",
|
||||
"code": 1354
|
||||
},
|
||||
"A 'const' assertion can only be applied to a string, number, boolean, array, or object literal.": {
|
||||
"category": "Error",
|
||||
"code": 1355
|
||||
},
|
||||
|
||||
"Duplicate identifier '{0}'.": {
|
||||
"category": "Error",
|
||||
|
||||
@@ -5607,6 +5607,11 @@ namespace ts {
|
||||
return node.kind === SyntaxKind.TypeAssertionExpression;
|
||||
}
|
||||
|
||||
export function isConstTypeReference(node: Node) {
|
||||
return isTypeReferenceNode(node) && isIdentifier(node.typeName) &&
|
||||
node.typeName.escapedText === "const" && !node.typeArguments;
|
||||
}
|
||||
|
||||
export function isParenthesizedExpression(node: Node): node is ParenthesizedExpression {
|
||||
return node.kind === SyntaxKind.ParenthesizedExpression;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user