microsoft / microsoft/TypeScript
type predicate-ness lost when using Function.prototype.bind
Nobody has claimed this yet.
- Dominant language
- Go
- Stars
- 111k
- Forks
- 14.3k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 132
Description
TypeScript Version: 3.2.4 or @next as of 2019-01-22
Search Terms:
Code
const isArray: typeof Array.isArray = Array.isArray.bind(Array);
Expected behavior:
Compiles.
Actual behavior:
Error message:
error TS2322: Type '(arg: any) => boolean' is not assignable to type '(arg: any) => arg is any[]'.
Signature '(arg: any): boolean' must be a type predicate.
What seems to be happening is that the type predicate-ness of Array.isArray is lost when .bind is used. I tested with a few type predicate functions, and always get the same error.
Playground Link:
Not reproducible on playground, because it depends on a higher TS version.
In the playground, the type of the RHS is any, so there is no compilation error.
Related Issues:
-
Typings for
Function.prototype.bindwere discussed in https://github.com/Microsoft/TypeScript/issues/212 but I couldn't find any mention of type predicates.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reproducing the TypeScript 3.2.4 example with Array.isArray.bind(Array) and compare its behavior with typescript@next. Trace how Function.prototype.bind preserves function signatures and type predicates; done means the example compiles without losing the predicate type.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- typescript
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100