Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Really fix type parameter of applyWithFunction #2660

Merged
merged 5 commits into from Aug 14, 2017
Merged

Really fix type parameter of applyWithFunction #2660

merged 5 commits into from Aug 14, 2017

Conversation

ajafff
Copy link
Contributor

@ajafff ajafff commented May 1, 2017

PR checklist

Overview of change:

Followup on #2396:
Using U extends T allows excess properties.
Using {} & T does stricter type checking (no more excess properties and enables linting the parameter with no-unsafe-any) and also fixes the inference problem with arrow functions: #2396 (comment)

Is there anything you'd like reviewers to focus on?

This change may technically break dependent libraries if they explicitly specified the type parameters. In reality possibly noone ever did that. On the other hand this change may reveal bugs during type checking.

CHANGELOG.md entry:

[api] AbstractRule#applyWithFunction has better type checking for its type parameter

@nchen63
Copy link
Contributor

nchen63 commented May 2, 2017

{} & T doesn't seem to be encouraged microsoft/TypeScript#14829 (comment)

@ajafff
Copy link
Contributor Author

ajafff commented Jul 28, 2017

Seems like this is now the preferred way to exclude a parameter from type inference. It's even suggested by TypeScript devs microsoft/TypeScript#17491 (comment)

@ajafff
Copy link
Contributor Author

ajafff commented Jul 31, 2017

CI failed to compile. Looks like this solution will not work for all cases.
I'll investigate further and try to find a working solution.

@ajafff
Copy link
Contributor Author

ajafff commented Aug 14, 2017

I managed to work around the type error. Cross posting microsoft/TypeScript#14829 (comment) for reference

T & {} only works when not using strictNullChecks.
I encountered a problem when T is undefined, because undefined is not assignable to undefined & {}.

I found that mapped types also defer inference, so i tried type InferLast<T> = Readonly<T>;
That does not work for types with index signatures like arrays.

My final solution is a mapped type intersected with the type itself:

export type NoInfer<T> = T & {[K in keyof T]: T[K]};

@adidahiya adidahiya merged commit d8b6225 into palantir:master Aug 14, 2017
HyphnKnight pushed a commit to HyphnKnight/tslint that referenced this pull request Apr 9, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants