Skip to content
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

Fixed contravariant inferences from annotated optional parameters #55397

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 3 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35120,7 +35120,9 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
if (declaration.type) {
const typeNode = getEffectiveTypeAnnotationNode(declaration);
if (typeNode) {
inferTypes(inferenceContext.inferences, getTypeFromTypeNode(typeNode), getTypeAtPosition(context, i));
const source = addOptionality(getTypeFromTypeNode(typeNode), /*isProperty*/ false, hasQuestionToken(declaration));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hasQuestionToken(declaration) isn't going to do the right thing for jsdoc optional parameters - instead, use the catch-all isOptionalDeclaration.

Copy link
Contributor Author

@Andarist Andarist Aug 16, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 pushed out the appropriate change (and a test case)

const target = getTypeAtPosition(context, i);
inferTypes(inferenceContext.inferences, source, target);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
//// [tests/cases/compiler/contravariantOnlyInferenceWithAnnotatedOptionalParameter.ts] ////

=== contravariantOnlyInferenceWithAnnotatedOptionalParameter.ts ===
// repro https://github.com/microsoft/TypeScript/issues/55394

declare function filter<T>(predicate: (value: T, index: number) => boolean): T;
>filter : Symbol(filter, Decl(contravariantOnlyInferenceWithAnnotatedOptionalParameter.ts, 0, 0))
>T : Symbol(T, Decl(contravariantOnlyInferenceWithAnnotatedOptionalParameter.ts, 2, 24))
>predicate : Symbol(predicate, Decl(contravariantOnlyInferenceWithAnnotatedOptionalParameter.ts, 2, 27))
>value : Symbol(value, Decl(contravariantOnlyInferenceWithAnnotatedOptionalParameter.ts, 2, 39))
>T : Symbol(T, Decl(contravariantOnlyInferenceWithAnnotatedOptionalParameter.ts, 2, 24))
>index : Symbol(index, Decl(contravariantOnlyInferenceWithAnnotatedOptionalParameter.ts, 2, 48))
>T : Symbol(T, Decl(contravariantOnlyInferenceWithAnnotatedOptionalParameter.ts, 2, 24))

const a = filter((pose?: number) => true);
>a : Symbol(a, Decl(contravariantOnlyInferenceWithAnnotatedOptionalParameter.ts, 3, 5))
>filter : Symbol(filter, Decl(contravariantOnlyInferenceWithAnnotatedOptionalParameter.ts, 0, 0))
>pose : Symbol(pose, Decl(contravariantOnlyInferenceWithAnnotatedOptionalParameter.ts, 3, 18))

const b = filter((pose?: number, _?: number) => true);
>b : Symbol(b, Decl(contravariantOnlyInferenceWithAnnotatedOptionalParameter.ts, 4, 5))
>filter : Symbol(filter, Decl(contravariantOnlyInferenceWithAnnotatedOptionalParameter.ts, 0, 0))
>pose : Symbol(pose, Decl(contravariantOnlyInferenceWithAnnotatedOptionalParameter.ts, 4, 18))
>_ : Symbol(_, Decl(contravariantOnlyInferenceWithAnnotatedOptionalParameter.ts, 4, 32))

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//// [tests/cases/compiler/contravariantOnlyInferenceWithAnnotatedOptionalParameter.ts] ////

=== contravariantOnlyInferenceWithAnnotatedOptionalParameter.ts ===
// repro https://github.com/microsoft/TypeScript/issues/55394

declare function filter<T>(predicate: (value: T, index: number) => boolean): T;
>filter : <T>(predicate: (value: T, index: number) => boolean) => T
>predicate : (value: T, index: number) => boolean
>value : T
>index : number

const a = filter((pose?: number) => true);
>a : number | undefined
>filter((pose?: number) => true) : number | undefined
>filter : <T>(predicate: (value: T, index: number) => boolean) => T
>(pose?: number) => true : (pose?: number) => true
>pose : number | undefined
>true : true

const b = filter((pose?: number, _?: number) => true);
>b : number | undefined
>filter((pose?: number, _?: number) => true) : number | undefined
>filter : <T>(predicate: (value: T, index: number) => boolean) => T
>(pose?: number, _?: number) => true : (pose?: number, _?: number) => true
>pose : number | undefined
>_ : number | undefined
>true : true

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// @strict: true
// @noEmit: true

// repro https://github.com/microsoft/TypeScript/issues/55394

declare function filter<T>(predicate: (value: T, index: number) => boolean): T;
const a = filter((pose?: number) => true);
const b = filter((pose?: number, _?: number) => true);
Loading