Skip to content

Commit

Permalink
Fixed contravariant inferences from annotated optional parameters (mi…
Browse files Browse the repository at this point in the history
  • Loading branch information
Andarist authored and snovader committed Sep 23, 2023
1 parent 7332862 commit 7b0d66e
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35528,7 +35528,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));
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
);

0 comments on commit 7b0d66e

Please sign in to comment.