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
12 changes: 4 additions & 8 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22983,10 +22983,10 @@ namespace ts {
return isLengthPushOrUnshift || isElementAssignment;
}

function isDeclarationWithExplicitTypeAnnotation(declaration: Declaration) {
return (declaration.kind === SyntaxKind.VariableDeclaration || declaration.kind === SyntaxKind.Parameter ||
declaration.kind === SyntaxKind.PropertyDeclaration || declaration.kind === SyntaxKind.PropertySignature) &&
!!getEffectiveTypeAnnotationNode(declaration as VariableDeclaration | ParameterDeclaration | PropertyDeclaration | PropertySignature);
function isDeclarationWithExplicitTypeAnnotation(node: Declaration) {
return (isVariableDeclaration(node) || isPropertyDeclaration(node) || isPropertySignature(node) || isParameter(node)) &&
!!(getEffectiveTypeAnnotationNode(node) ||
isInJSFile(node) && hasInitializer(node) && node.initializer && isFunctionExpressionOrArrowFunction(node.initializer) && getEffectiveReturnTypeNode(node.initializer));
}

function getExplicitTypeOfSymbol(symbol: Symbol, diagnostic?: Diagnostic) {
Expand Down Expand Up @@ -26322,10 +26322,6 @@ namespace ts {
return !hasEffectiveRestParameter(signature) && getParameterCount(signature) < targetParameterCount;
}

function isFunctionExpressionOrArrowFunction(node: Node): node is FunctionExpression | ArrowFunction {
return node.kind === SyntaxKind.FunctionExpression || node.kind === SyntaxKind.ArrowFunction;
}

function getContextualSignatureForFunctionLikeDeclaration(node: FunctionLikeDeclaration): Signature | undefined {
// Only function expressions, arrow functions, and object literal methods are contextually typed.
return isFunctionExpressionOrArrowFunction(node) || isObjectLiteralMethod(node)
Expand Down
4 changes: 4 additions & 0 deletions src/compiler/utilities.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7418,4 +7418,8 @@ namespace ts {
const declaration = symbol.valueDeclaration && getRootDeclaration(symbol.valueDeclaration);
return !!declaration && (isParameter(declaration) || isCatchClauseVariableDeclaration(declaration));
}

export function isFunctionExpressionOrArrowFunction(node: Node): node is FunctionExpression | ArrowFunction {
return node.kind === SyntaxKind.FunctionExpression || node.kind === SyntaxKind.ArrowFunction;
}
}
50 changes: 50 additions & 0 deletions tests/baselines/reference/assertionTypePredicates2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
//// [assertionTypePredicates2.js]
/**
* @typedef {{ x: number }} A
*/

/**
* @typedef { A & { y: number } } B
*/

/**
* @param {A} a
* @returns { asserts a is B }
*/
const foo = (a) => {
if (/** @type { B } */ (a).y !== 0) throw TypeError();
return undefined;
};

export const main = () => {
/** @type { A } */
const a = { x: 1 };
foo(a);
};


//// [assertionTypePredicates2.js]
"use strict";
/**
* @typedef {{ x: number }} A
*/
exports.__esModule = true;
exports.main = void 0;
/**
* @typedef { A & { y: number } } B
*/
/**
* @param {A} a
* @returns { asserts a is B }
*/
var foo = function (a) {
if ( /** @type { B } */(a).y !== 0)
throw TypeError();
return undefined;
};
var main = function () {
/** @type { A } */
var a = { x: 1 };
foo(a);
};
exports.main = main;
42 changes: 42 additions & 0 deletions tests/baselines/reference/assertionTypePredicates2.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
=== tests/cases/conformance/controlFlow/assertionTypePredicates2.js ===
/**
* @typedef {{ x: number }} A
*/

/**
* @typedef { A & { y: number } } B
*/

/**
* @param {A} a
* @returns { asserts a is B }
*/
const foo = (a) => {
>foo : Symbol(foo, Decl(assertionTypePredicates2.js, 12, 5))
>a : Symbol(a, Decl(assertionTypePredicates2.js, 12, 13))

if (/** @type { B } */ (a).y !== 0) throw TypeError();
>(a).y : Symbol(y, Decl(assertionTypePredicates2.js, 5, 19))
>a : Symbol(a, Decl(assertionTypePredicates2.js, 12, 13))
>y : Symbol(y, Decl(assertionTypePredicates2.js, 5, 19))
>TypeError : Symbol(TypeError, Decl(lib.es5.d.ts, --, --), Decl(lib.es5.d.ts, --, --))

return undefined;
>undefined : Symbol(undefined)

};

export const main = () => {
>main : Symbol(main, Decl(assertionTypePredicates2.js, 17, 12))

/** @type { A } */
const a = { x: 1 };
>a : Symbol(a, Decl(assertionTypePredicates2.js, 19, 9))
>x : Symbol(x, Decl(assertionTypePredicates2.js, 19, 15))

foo(a);
>foo : Symbol(foo, Decl(assertionTypePredicates2.js, 12, 5))
>a : Symbol(a, Decl(assertionTypePredicates2.js, 19, 9))

};

51 changes: 51 additions & 0 deletions tests/baselines/reference/assertionTypePredicates2.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
=== tests/cases/conformance/controlFlow/assertionTypePredicates2.js ===
/**
* @typedef {{ x: number }} A
*/

/**
* @typedef { A & { y: number } } B
*/

/**
* @param {A} a
* @returns { asserts a is B }
*/
const foo = (a) => {
>foo : (a: A) => asserts a is B
>(a) => { if (/** @type { B } */ (a).y !== 0) throw TypeError(); return undefined;} : (a: A) => asserts a is B
>a : A

if (/** @type { B } */ (a).y !== 0) throw TypeError();
>(a).y !== 0 : boolean
>(a).y : number
>(a) : B
>a : A
>y : number
>0 : 0
>TypeError() : TypeError
>TypeError : TypeErrorConstructor

return undefined;
>undefined : undefined

};

export const main = () => {
>main : () => void
>() => { /** @type { A } */ const a = { x: 1 }; foo(a);} : () => void

/** @type { A } */
const a = { x: 1 };
>a : A
>{ x: 1 } : { x: number; }
>x : number
>1 : 1

foo(a);
>foo(a) : void
>foo : (a: A) => asserts a is B
>a : A

};

27 changes: 27 additions & 0 deletions tests/cases/conformance/controlFlow/assertionTypePredicates2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// @allowJs: true
// @checkJs: true
// @outDir: ./out
// @filename: assertionTypePredicates2.js

/**
* @typedef {{ x: number }} A
*/

/**
* @typedef { A & { y: number } } B
*/

/**
* @param {A} a
* @returns { asserts a is B }
*/
const foo = (a) => {
if (/** @type { B } */ (a).y !== 0) throw TypeError();
return undefined;
};

export const main = () => {
/** @type { A } */
const a = { x: 1 };
foo(a);
};