Skip to content

Type predicate calling another predicate with different argument position #6311

Closed
@andy-hanson

Description

@andy-hanson

This code will not compile:

function isString1(a: number, b: Object): b is string {
    return typeof b === 'string'
}

function isString2(a: Object): a is string {
    return isString1(0, a)
}

Output:

foo.ts(6,9): error TS1226: Type predicate 'b is string' is not assignable to 'a is string'.
  Parameter 'b' is not in the same position as parameter 'a'.

The code will work if I swap the parameters of isString1 so that the predicated parameter for both functions is the first argument.
Is there a good reason that this is required?

Metadata

Metadata

Labels

BugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions