Skip to content

Improved errors for required parameters with default values in isolated declaration #58637

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

Merged
merged 1 commit into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 6 additions & 6 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6103,7 +6103,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
return result;
}
}
context.tracker.reportInferenceFallback(existing);
return undefined;
}

Expand Down Expand Up @@ -8204,6 +8203,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
function serializeTypeForDeclaration(context: NodeBuilderContext, declaration: Declaration | undefined, type: Type, symbol: Symbol) {
const addUndefined = declaration && (isParameter(declaration) || isJSDocParameterTag(declaration)) && requiresAddingImplicitUndefined(declaration);
const enclosingDeclaration = context.enclosingDeclaration;
const oldFlags = context.flags;
if (declaration && hasInferredType(declaration) && !(context.flags & NodeBuilderFlags.NoSyntacticPrinter)) {
syntacticNodeBuilder.serializeTypeOfDeclaration(declaration, context);
}
context.flags |= NodeBuilderFlags.NoSyntacticPrinter;
if (enclosingDeclaration && (!isErrorType(type) || (context.flags & NodeBuilderFlags.AllowUnresolvedNames))) {
const declWithExistingAnnotation = declaration && getNonlocalEffectiveTypeAnnotationNode(declaration)
? declaration
Expand All @@ -8213,11 +8217,11 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const existing = getNonlocalEffectiveTypeAnnotationNode(declWithExistingAnnotation)!;
const result = !isTypePredicateNode(existing) && tryReuseExistingTypeNode(context, existing, type, declWithExistingAnnotation, addUndefined);
if (result) {
context.flags = oldFlags;
return result;
}
}
}
const oldFlags = context.flags;
if (
type.flags & TypeFlags.UniqueESSymbol &&
type.symbol === symbol && (!context.enclosingDeclaration || some(symbol.declarations, d => getSourceFileOfNode(d) === getSourceFileOfNode(context.enclosingDeclaration!)))
Expand All @@ -8228,10 +8232,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
const decl = declaration ?? symbol.valueDeclaration ?? symbol.declarations?.[0];
const expr = decl && isDeclarationWithPossibleInnerTypeNodeReuse(decl) ? getPossibleTypeNodeReuseExpression(decl) : undefined;

if (decl && hasInferredType(decl) && !(context.flags & NodeBuilderFlags.NoSyntacticPrinter)) {
syntacticNodeBuilder.serializeTypeOfDeclaration(decl, context);
}
context.flags |= NodeBuilderFlags.NoSyntacticPrinter;
const result = expressionOrTypeToTypeNode(context, expr, type, addUndefined);
context.flags = oldFlags;
return result;
Expand Down
15 changes: 7 additions & 8 deletions src/compiler/expressionToTypeNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import {
isIdentifier,
isJSDocTypeAssertion,
isKeyword,
isParameter,
isPrimitiveLiteralValue,
isShorthandPropertyAssignment,
isSpreadAssignment,
Expand Down Expand Up @@ -76,8 +75,8 @@ export function createSyntacticTypeNodeBuilder(options: CompilerOptions, resolve
serializeReturnTypeForSignature,
serializeTypeOfExpression,
};
function serializeExistingTypeAnnotation(type: TypeNode | undefined) {
return type === undefined ? undefined : !type.parent || !isParameter(type.parent) || !resolver.requiresAddingImplicitUndefined(type.parent) || canAddUndefined(type);
function serializeExistingTypeAnnotation(type: TypeNode | undefined, addUndefined?: boolean) {
return type !== undefined && (!addUndefined || (type && canAddUndefined(type))) ? true : undefined;
}
function serializeTypeOfExpression(expr: Expression, context: SyntacticTypeNodeBuilderContext, addUndefined?: boolean, preserveLiterals?: boolean) {
return typeFromExpression(expr, context, /*isConstContext*/ false, addUndefined, preserveLiterals) ?? inferExpressionType(expr, context);
Expand Down Expand Up @@ -181,12 +180,12 @@ export function createSyntacticTypeNodeBuilder(options: CompilerOptions, resolve
const declaredType = getEffectiveTypeAnnotationNode(node);
const addUndefined = resolver.requiresAddingImplicitUndefined(node);
let resultType;
if (!addUndefined) {
if (declaredType) {
return serializeExistingTypeAnnotation(declaredType);
}
if (declaredType) {
resultType = serializeExistingTypeAnnotation(declaredType, addUndefined);
}
else {
if (node.initializer && isIdentifier(node.name)) {
resultType = typeFromExpression(node.initializer, context);
resultType = typeFromExpression(node.initializer, context, /*isConstContext*/ undefined, addUndefined);
}
}
return resultType ?? inferTypeOfDeclaration(node, context);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
file2.ts(1,26): error TS9025: Declaration emit for this parameter requires implicitly adding undefined to it's type. This is not supported with --isolatedDeclarations.
file2.ts(4,38): error TS9011: Parameter must have an explicit type annotation with --isolatedDeclarations.


==== file1.ts (0 errors) ====
Expand All @@ -12,10 +12,12 @@ file2.ts(1,26): error TS9025: Declaration emit for this parameter requires impli

==== file2.ts (1 errors) ====
export function foo(p = (ip = 10, v: number): void => {}): void{
~~~~~~~
!!! error TS9025: Declaration emit for this parameter requires implicitly adding undefined to it's type. This is not supported with --isolatedDeclarations.
!!! related TS9028 file2.ts:1:26: Add a type annotation to the parameter ip.
}
type T = number
export function foo2(p = (ip = 10 as T, v: number): void => {}): void{}
~
!!! error TS9011: Parameter must have an explicit type annotation with --isolatedDeclarations.
!!! related TS9028 file2.ts:4:27: Add a type annotation to the parameter ip.
export class Bar2 {
readonly r = 1;
f = 2;
Expand Down
8 changes: 8 additions & 0 deletions tests/baselines/reference/isolatedDeclarationsAddUndefined.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export class Bar {
//// [file2.ts]
export function foo(p = (ip = 10, v: number): void => {}): void{
}
type T = number
export function foo2(p = (ip = 10 as T, v: number): void => {}): void{}
export class Bar2 {
readonly r = 1;
f = 2;
Expand All @@ -36,11 +38,17 @@ exports.Bar = Bar;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Bar2 = void 0;
exports.foo = foo;
exports.foo2 = foo2;
function foo(p) {
if (p === void 0) { p = function (ip, v) {
if (ip === void 0) { ip = 10; }
}; }
}
function foo2(p) {
if (p === void 0) { p = function (ip, v) {
if (ip === void 0) { ip = 10; }
}; }
}
var Bar2 = /** @class */ (function () {
function Bar2() {
this.r = 1;
Expand Down
16 changes: 13 additions & 3 deletions tests/baselines/reference/isolatedDeclarationsAddUndefined.symbols
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,22 @@ export function foo(p = (ip = 10, v: number): void => {}): void{
>ip : Symbol(ip, Decl(file2.ts, 0, 25))
>v : Symbol(v, Decl(file2.ts, 0, 33))
}
type T = number
>T : Symbol(T, Decl(file2.ts, 1, 1))

export function foo2(p = (ip = 10 as T, v: number): void => {}): void{}
>foo2 : Symbol(foo2, Decl(file2.ts, 2, 15))
>p : Symbol(p, Decl(file2.ts, 3, 21))
>ip : Symbol(ip, Decl(file2.ts, 3, 26))
>T : Symbol(T, Decl(file2.ts, 1, 1))
>v : Symbol(v, Decl(file2.ts, 3, 39))

export class Bar2 {
>Bar2 : Symbol(Bar2, Decl(file2.ts, 1, 1))
>Bar2 : Symbol(Bar2, Decl(file2.ts, 3, 71))

readonly r = 1;
>r : Symbol(Bar2.r, Decl(file2.ts, 2, 19))
>r : Symbol(Bar2.r, Decl(file2.ts, 4, 19))

f = 2;
>f : Symbol(Bar2.f, Decl(file2.ts, 3, 19))
>f : Symbol(Bar2.f, Decl(file2.ts, 5, 19))
}
20 changes: 20 additions & 0 deletions tests/baselines/reference/isolatedDeclarationsAddUndefined.types
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,26 @@ export function foo(p = (ip = 10, v: number): void => {}): void{
>v : number
> : ^^^^^^
}
type T = number
>T : number
> : ^^^^^^

export function foo2(p = (ip = 10 as T, v: number): void => {}): void{}
>foo2 : (p?: (ip: T | undefined, v: number) => void) => void
> : ^ ^^^^ ^^ ^^^^^^^^^^^^^^ ^^ ^^^^^ ^^^^^
>p : (ip: T | undefined, v: number) => void
> : ^ ^^ ^^^^^^^^^^^^^^ ^^ ^^^^^
>(ip = 10 as T, v: number): void => {} : (ip: T | undefined, v: number) => void
> : ^ ^^ ^^^^^^^^^^^^^^ ^^ ^^^^^
>ip : number
> : ^^^^^^
>10 as T : number
> : ^^^^^^
>10 : 10
> : ^^
>v : number
> : ^^^^^^

export class Bar2 {
>Bar2 : Bar2
> : ^^^^
Expand Down
Loading