Skip to content
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
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, isOptionalDeclaration(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,41 @@
//// [tests/cases/compiler/contravariantOnlyInferenceWithAnnotatedOptionalParameterJs.ts] ////

=== index.js ===
/**
* @template T
* @param {(value: T, index: number) => boolean} predicate
* @returns {T}
*/
function filter(predicate) {
>filter : Symbol(filter, Decl(index.js, 0, 0))
>predicate : Symbol(predicate, Decl(index.js, 5, 16))

return /** @type {any} */ (null);
}

const a = filter(
>a : Symbol(a, Decl(index.js, 9, 5))
>filter : Symbol(filter, Decl(index.js, 0, 0))

/**
* @param {number} [pose]
*/
(pose) => true
>pose : Symbol(pose, Decl(index.js, 13, 3))

);

const b = filter(
>b : Symbol(b, Decl(index.js, 16, 5))
>filter : Symbol(filter, Decl(index.js, 0, 0))

/**
* @param {number} [pose]
* @param {number} [_]
*/
(pose, _) => true
>pose : Symbol(pose, Decl(index.js, 21, 3))
>_ : Symbol(_, Decl(index.js, 21, 8))

);

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

=== index.js ===
/**
* @template T
* @param {(value: T, index: number) => boolean} predicate
* @returns {T}
*/
function filter(predicate) {
>filter : <T>(predicate: (value: T, index: number) => boolean) => T
>predicate : (value: T, index: number) => boolean

return /** @type {any} */ (null);
>(null) : any
}

const a = filter(
>a : number | undefined
>filter( /** * @param {number} [pose] */ (pose) => true) : number | undefined
>filter : <T>(predicate: (value: T, index: number) => boolean) => T

/**
* @param {number} [pose]
*/
(pose) => true
>(pose) => true : (pose?: number | undefined) => true
>pose : number | undefined
>true : true

);

const b = filter(
>b : number | undefined
>filter( /** * @param {number} [pose] * @param {number} [_] */ (pose, _) => true) : number | undefined
>filter : <T>(predicate: (value: T, index: number) => boolean) => T

/**
* @param {number} [pose]
* @param {number} [_]
*/
(pose, _) => true
>(pose, _) => true : (pose?: number | undefined, _?: number | undefined) => 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);
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// @strict: true
// @checkJs: true
// @noEmit: true

// @filename: index.js

/**
* @template T
* @param {(value: T, index: number) => boolean} predicate
* @returns {T}
*/
function filter(predicate) {
return /** @type {any} */ (null);
}

const a = filter(
/**
* @param {number} [pose]
*/
(pose) => true
);

const b = filter(
/**
* @param {number} [pose]
* @param {number} [_]
*/
(pose, _) => true
);