Skip to content

Single-expression only type predicates #57552

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

Closed
Closed
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
92 changes: 89 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15458,9 +15458,19 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
jsdocPredicate = getTypePredicateOfSignature(jsdocSignature);
}
}
signature.resolvedTypePredicate = type && isTypePredicateNode(type) ?
createTypePredicateFromTypePredicateNode(type, signature) :
jsdocPredicate || noTypePredicate;
if (type || jsdocPredicate) {
signature.resolvedTypePredicate = type && isTypePredicateNode(type) ?
createTypePredicateFromTypePredicateNode(type, signature) :
jsdocPredicate || noTypePredicate;
}
else if (signature.declaration && isFunctionLikeDeclaration(signature.declaration) && (!signature.resolvedReturnType || signature.resolvedReturnType === booleanType)) {
const { declaration } = signature;
signature.resolvedTypePredicate = noTypePredicate; // avoid infinite loop
signature.resolvedTypePredicate = getTypePredicateFromBody(declaration) || noTypePredicate;
}
else {
signature.resolvedTypePredicate = noTypePredicate;
}
}
Debug.assert(!!signature.resolvedTypePredicate);
}
Expand Down Expand Up @@ -37389,6 +37399,82 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
}
}

function getTypePredicateFromBody(func: FunctionLikeDeclaration): TypePredicate | undefined {
if (func.kind === SyntaxKind.Constructor || func.kind === SyntaxKind.SetAccessor) {
return undefined;
}

// The body must be an expression, or a block containing a lone return statement
if (!func.body) return undefined;

const functionFlags = getFunctionFlags(func);
// Only a normal single-argument function can be inferred to be a type predicate
if (functionFlags !== FunctionFlags.Normal || func.parameters.length !== 1) return undefined;

let returnExpression: Expression | undefined;
if (func.body.kind === SyntaxKind.Block) {
const body = func.body as Block;
if ((body.statements.length !== 1) || (body.statements[0].kind !== SyntaxKind.ReturnStatement)) return undefined;
returnExpression = (body.statements[0] as ReturnStatement).expression;
}
else {
returnExpression = func.body;
}
if (!returnExpression) return undefined;

const predicate = checkIfExpressionRefinesAnyParameter(returnExpression);
if (predicate) {
const [i, _type] = predicate;
const param = func.parameters[i];
if (isIdentifier(param.name)) {
// TODO: is there an alternative to the "as string" here? (It's __String)
// return createTypePredicate(TypePredicateKind.Identifier, param.name.escapedText as string, i, type);
return undefined;
}
}
return undefined;

function checkIfExpressionRefinesAnyParameter(expr: Expression): [number, Type] | undefined {
expr = skipParentheses(expr, /*excludeJSDocTypeAssertions*/ true);
const type = checkExpressionCached(expr);
if (type !== booleanType) return undefined;

return forEach(func.parameters, (param, i) => {
const initType = getSymbolLinks(param.symbol).type;
if (!initType || initType === booleanType || isSymbolAssigned(param.symbol)) {
// Refining "x: boolean" to "x is true" or "x is false" isn't useful.
return;
}
const trueType = checkIfExpressionRefinesParameter(expr, param, initType);
if (trueType) {
return [i, trueType];
}
});
}

function checkIfExpressionRefinesParameter(expr: Expression, param: ParameterDeclaration, initType: Type): Type | undefined {
const antecedent = (expr as Expression & { flowNode?: FlowNode; }).flowNode ?? { flags: FlowFlags.Start };
const trueCondition: FlowCondition = {
flags: FlowFlags.TrueCondition,
node: expr,
antecedent,
};

const trueType = getFlowTypeOfReference(param.name, initType, initType, func, trueCondition);
if (trueType === initType) return undefined;

// "x is T" means that x is T if and only if it returns true. If it returns false then x is not T.
// This means that if the function is called with an argument of type trueType, there can't be anything left in the `else` branch. It must reduce to `never`.
const falseCondition: FlowCondition = {
...trueCondition,
flags: FlowFlags.FalseCondition,
};
const falseSubtype = getFlowTypeOfReference(param.name, trueType, trueType, func, falseCondition);
if (!isTypeIdenticalTo(falseSubtype, neverType)) return undefined;
return trueType;
}
}

/**
* TypeScript Specification 1.0 (6.3) - July 2014
* An explicitly typed function whose return type isn't the Void type,
Expand Down
41 changes: 41 additions & 0 deletions tests/baselines/reference/circularConstructorWithReturn.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//// [tests/cases/compiler/circularConstructorWithReturn.ts] ////

//// [circularConstructorWithReturn.ts]
// This should not be a circularity error. See
// https://github.com/microsoft/TypeScript/pull/57465#issuecomment-1960271216
export type Client = ReturnType<typeof getPrismaClient> extends new () => infer T ? T : never

export function getPrismaClient(options?: any) {
class PrismaClient {
self: Client;
constructor(options?: any) {
return (this.self = applyModelsAndClientExtensions(this));
}
}

return PrismaClient
}

export function applyModelsAndClientExtensions(client: Client) {
return client;
}


//// [circularConstructorWithReturn.js]
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.applyModelsAndClientExtensions = exports.getPrismaClient = void 0;
function getPrismaClient(options) {
var PrismaClient = /** @class */ (function () {
function PrismaClient(options) {
return (this.self = applyModelsAndClientExtensions(this));
}
return PrismaClient;
}());
return PrismaClient;
}
exports.getPrismaClient = getPrismaClient;
function applyModelsAndClientExtensions(client) {
return client;
}
exports.applyModelsAndClientExtensions = applyModelsAndClientExtensions;
48 changes: 48 additions & 0 deletions tests/baselines/reference/circularConstructorWithReturn.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//// [tests/cases/compiler/circularConstructorWithReturn.ts] ////

=== circularConstructorWithReturn.ts ===
// This should not be a circularity error. See
// https://github.com/microsoft/TypeScript/pull/57465#issuecomment-1960271216
export type Client = ReturnType<typeof getPrismaClient> extends new () => infer T ? T : never
>Client : Symbol(Client, Decl(circularConstructorWithReturn.ts, 0, 0))
>ReturnType : Symbol(ReturnType, Decl(lib.es5.d.ts, --, --))
>getPrismaClient : Symbol(getPrismaClient, Decl(circularConstructorWithReturn.ts, 2, 93))
>T : Symbol(T, Decl(circularConstructorWithReturn.ts, 2, 79))
>T : Symbol(T, Decl(circularConstructorWithReturn.ts, 2, 79))

export function getPrismaClient(options?: any) {
>getPrismaClient : Symbol(getPrismaClient, Decl(circularConstructorWithReturn.ts, 2, 93))
>options : Symbol(options, Decl(circularConstructorWithReturn.ts, 4, 32))

class PrismaClient {
>PrismaClient : Symbol(PrismaClient, Decl(circularConstructorWithReturn.ts, 4, 48))

self: Client;
>self : Symbol(PrismaClient.self, Decl(circularConstructorWithReturn.ts, 5, 22))
>Client : Symbol(Client, Decl(circularConstructorWithReturn.ts, 0, 0))

constructor(options?: any) {
>options : Symbol(options, Decl(circularConstructorWithReturn.ts, 7, 16))

return (this.self = applyModelsAndClientExtensions(this));
>this.self : Symbol(PrismaClient.self, Decl(circularConstructorWithReturn.ts, 5, 22))
>this : Symbol(PrismaClient, Decl(circularConstructorWithReturn.ts, 4, 48))
>self : Symbol(PrismaClient.self, Decl(circularConstructorWithReturn.ts, 5, 22))
>applyModelsAndClientExtensions : Symbol(applyModelsAndClientExtensions, Decl(circularConstructorWithReturn.ts, 13, 1))
>this : Symbol(PrismaClient, Decl(circularConstructorWithReturn.ts, 4, 48))
}
}

return PrismaClient
>PrismaClient : Symbol(PrismaClient, Decl(circularConstructorWithReturn.ts, 4, 48))
}

export function applyModelsAndClientExtensions(client: Client) {
>applyModelsAndClientExtensions : Symbol(applyModelsAndClientExtensions, Decl(circularConstructorWithReturn.ts, 13, 1))
>client : Symbol(client, Decl(circularConstructorWithReturn.ts, 15, 47))
>Client : Symbol(Client, Decl(circularConstructorWithReturn.ts, 0, 0))

return client;
>client : Symbol(client, Decl(circularConstructorWithReturn.ts, 15, 47))
}

46 changes: 46 additions & 0 deletions tests/baselines/reference/circularConstructorWithReturn.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//// [tests/cases/compiler/circularConstructorWithReturn.ts] ////

=== circularConstructorWithReturn.ts ===
// This should not be a circularity error. See
// https://github.com/microsoft/TypeScript/pull/57465#issuecomment-1960271216
export type Client = ReturnType<typeof getPrismaClient> extends new () => infer T ? T : never
>Client : PrismaClient
>getPrismaClient : (options?: any) => typeof PrismaClient

export function getPrismaClient(options?: any) {
>getPrismaClient : (options?: any) => typeof PrismaClient
>options : any

class PrismaClient {
>PrismaClient : PrismaClient

self: Client;
>self : PrismaClient

constructor(options?: any) {
>options : any

return (this.self = applyModelsAndClientExtensions(this));
>(this.self = applyModelsAndClientExtensions(this)) : PrismaClient
>this.self = applyModelsAndClientExtensions(this) : PrismaClient
>this.self : PrismaClient
>this : this
>self : PrismaClient
>applyModelsAndClientExtensions(this) : PrismaClient
>applyModelsAndClientExtensions : (client: PrismaClient) => PrismaClient
>this : this
}
}

return PrismaClient
>PrismaClient : typeof PrismaClient
}

export function applyModelsAndClientExtensions(client: Client) {
>applyModelsAndClientExtensions : (client: Client) => PrismaClient
>client : PrismaClient

return client;
>client : PrismaClient
}

Loading