-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Inferred type parameter is too narrow with strictFunctionTypes enabled and branded types #49924
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
Comments
This is working as intended. We infer the more specific declare function tryCast<TOut extends T, TIn extends T, T = any>(value: TIn | undefined, test: (value: T) => value is TOut): TOut | undefined;
// ...
const maybeClassStatement = tryCast(statement, isClassLike); // infers tryCast<ClassLike, Statement, Node>(...) |
Cool, I'll give it a try on my branch where I'm trying to enable this option. |
Unfortunately that has a bad interaction with generic type guards. For example, this also from the TS repo for our parenthesizer rules: declare function cast<TOut extends T, TIn extends T, T = any>(value: TIn | undefined, test: (value: T) => value is TOut): TOut;
interface Node {
kind: number;
}
interface TypeNode extends Node {
typeInfo: string;
}
interface NodeArray<T extends Node> extends Array<T> {
someProp: string;
}
declare function isNodeArray<T extends Node>(array: readonly T[]): array is NodeArray<T>;
declare const types: readonly TypeNode[];
const x = cast(types, isNodeArray); // bad: NodeAray<Node>
declare function oldCast<TOut extends TIn, TIn = any>(value: TIn | undefined, test: (value: TIn) => value is TOut): TOut;
const y = oldCast(types, isNodeArray); // good: NodeArray<TypeNode> |
Bug Report
π Search Terms
strictFunctionTypes inferred type parameter too narrow branded types
π Version & Regression Information
β― Playground Link
Playground Link
π» Code
This is basically what I see in the TS repo.
π Actual behavior
The second type parameter to
tryCast
is inferred to beStatement
, 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.The text was updated successfully, but these errors were encountered: