mirror of
https://github.com/microsoft/TypeScript.git
synced 2025-11-18 17:21:48 +00:00
Fix39458 (#39508)
* remove undefined from optional parameter * accept baselines * use getTypeWithFacts
This commit is contained in:
@@ -5094,6 +5094,9 @@ namespace ts {
|
||||
if (parameterDeclaration && isRequiredInitializedParameter(parameterDeclaration)) {
|
||||
parameterType = getOptionalType(parameterType);
|
||||
}
|
||||
if ((context.flags & NodeBuilderFlags.NoUndefinedOptionalParameterType) && parameterDeclaration && !isJSDocParameterTag(parameterDeclaration) && isOptionalUninitializedParameter(parameterDeclaration)) {
|
||||
parameterType = getTypeWithFacts(parameterType, TypeFacts.NEUndefined);
|
||||
}
|
||||
const parameterTypeNode = serializeTypeForDeclaration(context, parameterType, parameterSymbol, context.enclosingDeclaration, privateSymbolVisitor, bundledImports);
|
||||
|
||||
const modifiers = !(context.flags & NodeBuilderFlags.OmitParameterModifiers) && preserveModifierFlags && parameterDeclaration && parameterDeclaration.modifiers ? parameterDeclaration.modifiers.map(factory.cloneNode) : undefined;
|
||||
@@ -36958,6 +36961,12 @@ namespace ts {
|
||||
hasSyntacticModifier(parameter, ModifierFlags.ParameterPropertyModifier);
|
||||
}
|
||||
|
||||
function isOptionalUninitializedParameter(parameter: ParameterDeclaration) {
|
||||
return !!strictNullChecks &&
|
||||
isOptionalParameter(parameter) &&
|
||||
!parameter.initializer;
|
||||
}
|
||||
|
||||
function isExpandoFunctionDeclaration(node: Declaration): boolean {
|
||||
const declaration = getParseTreeNode(node, isFunctionDeclaration);
|
||||
if (!declaration) {
|
||||
|
||||
@@ -4160,6 +4160,7 @@ namespace ts {
|
||||
UseAliasDefinedOutsideCurrentScope = 1 << 14, // Allow non-visible aliases
|
||||
UseSingleQuotesForStringLiteralType = 1 << 28, // Use single quotes for string literal type
|
||||
NoTypeReduction = 1 << 29, // Don't call getReducedType
|
||||
NoUndefinedOptionalParameterType = 1 << 30, // Do not add undefined to optional parameter type
|
||||
|
||||
// Error handling
|
||||
AllowThisInObjectLiteral = 1 << 15,
|
||||
|
||||
@@ -164,7 +164,7 @@ namespace ts.codefix {
|
||||
const program = context.program;
|
||||
const checker = program.getTypeChecker();
|
||||
const scriptTarget = getEmitScriptTarget(program.getCompilerOptions());
|
||||
const flags = NodeBuilderFlags.NoTruncation | NodeBuilderFlags.SuppressAnyReturnType | (quotePreference === QuotePreference.Single ? NodeBuilderFlags.UseSingleQuotesForStringLiteralType : 0);
|
||||
const flags = NodeBuilderFlags.NoTruncation | NodeBuilderFlags.NoUndefinedOptionalParameterType | NodeBuilderFlags.SuppressAnyReturnType | (quotePreference === QuotePreference.Single ? NodeBuilderFlags.UseSingleQuotesForStringLiteralType : 0);
|
||||
const signatureDeclaration = <MethodDeclaration>checker.signatureToSignatureDeclaration(signature, SyntaxKind.MethodDeclaration, enclosingDeclaration, flags, getNoopSymbolTrackerWithResolver(context));
|
||||
if (!signatureDeclaration) {
|
||||
return undefined;
|
||||
|
||||
@@ -2232,6 +2232,7 @@ declare namespace ts {
|
||||
UseAliasDefinedOutsideCurrentScope = 16384,
|
||||
UseSingleQuotesForStringLiteralType = 268435456,
|
||||
NoTypeReduction = 536870912,
|
||||
NoUndefinedOptionalParameterType = 1073741824,
|
||||
AllowThisInObjectLiteral = 32768,
|
||||
AllowQualifedNameInPlaceOfIdentifier = 65536,
|
||||
AllowAnonymousIdentifier = 131072,
|
||||
|
||||
@@ -2232,6 +2232,7 @@ declare namespace ts {
|
||||
UseAliasDefinedOutsideCurrentScope = 16384,
|
||||
UseSingleQuotesForStringLiteralType = 268435456,
|
||||
NoTypeReduction = 536870912,
|
||||
NoUndefinedOptionalParameterType = 1073741824,
|
||||
AllowThisInObjectLiteral = 32768,
|
||||
AllowQualifedNameInPlaceOfIdentifier = 65536,
|
||||
AllowAnonymousIdentifier = 131072,
|
||||
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
/// <reference path="fourslash.ts" />
|
||||
|
||||
////interface IFoo {
|
||||
//// bar(x?: number | string): void;
|
||||
////}
|
||||
////
|
||||
////class Foo implements IFoo {
|
||||
////}
|
||||
|
||||
//https://github.com/microsoft/TypeScript/issues/39458
|
||||
verify.codeFix({
|
||||
description: [ts.Diagnostics.Implement_interface_0.message, "IFoo"],
|
||||
newFileContent:
|
||||
`interface IFoo {
|
||||
bar(x?: number | string): void;
|
||||
}
|
||||
|
||||
class Foo implements IFoo {
|
||||
bar(x?: string | number): void {
|
||||
throw new Error("Method not implemented.");
|
||||
}
|
||||
}`,
|
||||
});
|
||||
Reference in New Issue
Block a user