Skip to content

Sub-optimal type parameter inference with strictFunctionTypes enabled #52111

Description

These are cases I've extracted from #49929; they are cases where if you don't specify the type parameters yourself, the inference isn't good.

This is potentially a full duplicate of #49924, however, the fix suggested in that issue does not work properly in our codebase. To test these, it may be easiest to just clone #49929 with the explicit type parameters removed and see what works and what doesn't.

enumSyntaxKind{Block,Identifier,CaseClause,FunctionExpression,FunctionDeclaration,}interfaceNode{kind: SyntaxKind;}interfaceExpressionextendsNode{_expressionBrand: any;}interfaceDeclarationextendsNode{_declarationBrand: any;}interfaceBlockextendsNode{kind: SyntaxKind.Block;}interfaceIdentifierextendsExpression,Declaration{kind: SyntaxKind.Identifier;}interfaceCaseClauseextendsNode{kind: SyntaxKind.CaseClause;}interfaceFunctionDeclarationextendsDeclaration{kind: SyntaxKind.FunctionDeclaration;}typeHasLocals=Block|FunctionDeclaration;declarefunctioncanHaveLocals(node: Node): node is HasLocals;declarefunctionassertNode<TextendsNode,UextendsT>(node: T|undefined,test: (node: T)=>node is U): asserts node is U;declarefunctionassertNode(node: Node|undefined,test: ((node: Node)=>boolean)|undefined): void;functionfoo(node: FunctionDeclaration|CaseClause){assertNode(node,canHaveLocals)node;// ^?assertNode<Node,HasLocals>(node,canHaveLocals)node;// ^?}declarefunctionisExpression(node: Node): node is Expression;declarefunctioncast<TOutextendsTIn,TIn=any>(value: TIn|undefined,test: (value: TIn)=>value is TOut): TOut;functionbar(node: Identifier|FunctionDeclaration){consta=cast(node,isExpression);// ^?constb=cast<Expression>(node,isExpression);// ^?}
Output
"use strict";varSyntaxKind;(function(SyntaxKind){SyntaxKind[SyntaxKind["Block"]=0]="Block";SyntaxKind[SyntaxKind["Identifier"]=1]="Identifier";SyntaxKind[SyntaxKind["CaseClause"]=2]="CaseClause";SyntaxKind[SyntaxKind["FunctionExpression"]=3]="FunctionExpression";SyntaxKind[SyntaxKind["FunctionDeclaration"]=4]="FunctionDeclaration";})(SyntaxKind||(SyntaxKind={}));functionfoo(node){assertNode(node,canHaveLocals);node;// ^?assertNode(node,canHaveLocals);node;// ^?}functionbar(node){consta=cast(node,isExpression);// ^?constb=cast(node,isExpression);// ^?}
Compiler Options
{
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"strictNullChecks": true,
"strictFunctionTypes": true,
"strictPropertyInitialization": true,
"strictBindCallApply": true,
"noImplicitThis": true,
"noImplicitReturns": true,
"alwaysStrict": true,
"esModuleInterop": true,
"declaration": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"target": "ES2017",
"jsx": "react",
"module": "ESNext",
"moduleResolution": "node"
}
}

Playground Link:Provided

Metadata

Metadata

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions