Skip to content

Inferred type parameter is too narrow with strictFunctionTypes enabled and branded types #49924

Description

Bug Report

🔎 Search Terms

strictFunctionTypes inferred type parameter too narrow branded types

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________

⏯ Playground Link

Playground Link

💻 Code

This is basically what I see in the TS repo.

exportfunctiontryCast<TOutextendsTIn,TIn=any>(value: TIn|undefined,test: (value: TIn)=>value is TOut): TOut|undefined;exportfunctiontryCast<T>(value: T,test: (value: T)=>boolean): T|undefined;exportfunctiontryCast<T>(value: T,test: (value: T)=>boolean): T|undefined{returnvalue!==undefined&&test(value) ? value : undefined;}constenumSyntaxKind{ClassExpression,ClassStatement,}interfaceNode{kind: SyntaxKind;}interfaceStatementextendsNode{_statementBrand: any;}interfaceClassExpressionextendsNode{kind: SyntaxKind.ClassExpression;}interfaceClassStatementextendsStatement{kind: SyntaxKind.ClassStatement;}typeClassLike=ClassExpression|ClassStatement;declarefunctionisClassLike(node: Node): node is ClassLike;declareconststatement: Statement|undefined;constmaybeClassStatement=tryCast(statement,isClassLike);constmaybeClassStatement2=tryCast<ClassLike,Node>(statement,isClassLike);

🙁 Actual behavior

The second type parameter to tryCast is inferred to be Statement, which is too narrow.

🙂 Expected behavior

The inferred type should be Node. This helper is used in the TS repo, but can't be used in some conditions because inference is wrong.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions