-
Notifications
You must be signed in to change notification settings - Fork 3k
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
Update of.ts #5027
Update of.ts #5027
Conversation
Remove valid use of `of`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Actually, the more I think about it, we probably just need to switch the order of the overloads but keep the deprecation in for the |
Yeah. I agree. Might as well move both of the deprecated signatures so that they follow the rest-parameter signature. They'll only be matched if an explicit type argument is specified. Although, could that break anyone doing |
Done, I wish there was a way to add a test for this. The closest I can do is mock the state here and link to a playground where it works declare function of(): 'OK';
declare function of<A extends Array<any>>(...args: A): 'OK';
declare function of<T>(): 'deprecated';
declare function of<T>(value: T): 'deprecated';
of() === 'OK';
of(1) === 'OK';
of(1, 'test') === 'OK';
of<number>() === 'deprecated';
of<number>(1) === 'deprecated';
of() === 'deprecated'; // sanity check, this line is a type error. |
|
Yeah. I'm inclined to look into adding another assertion to dtslint. I guess it should be possible to add It would also be useful to have its dual: |
This PR may not be ready due to microsoft/TypeScript#33597 We may need to keep the original order and just remove the deprecation warning for |
@kolodny @benlesh export function of<T>(...args: T[]): Observable<T>;
Never mind. I just remembered why. It's one of TypeScript's we-decided-it-should-behave-this-way 'features' (see microsoft/TypeScript#30824): declare function f<T>(...args: T[]): T;
const r = f(1, "2", "3"); // Argument of type '"2"' is not assignable to parameter of type '1' IMO, this behaviour, in conjunction with microsoft/TypeScript#33597, means that this part of TS is essentially broken. 😡 |
I believe this PR is ready to go. This will be a slight breaking change to folks doing
But other than that, there shouldn't be much impact. This issue only affected 30 out of ~100k targets within Google |
Given that TS doesn't behave particularly nicely with rest parameters and literals, I'm not convinced that we should strive to remove the overload signatures for E.g. export function of(): Observable<never>;
export function of<T>(value: T): Observable<T>;
export function of<T1, T2>(value1: T1, value2: T2): Observable<T1 | T2>;
// maybe up to T3?
export function of<A extends Array<any>>(...args: A): Observable<ValueFromArray<A>>; This seems a small price to pay to get it to work properly in 95% of cases - and there are only a few RxJS functions that take values like this. IMO, the TS behaviour is not going to change anytime soon. |
I'm OK with that, however I'd suggest we get creative with overloads to make the deprecated usage of declare function of<_T, T extends _T>(t: T): 'OK';
declare function of<T>(t: T): 'deprecated';
const ok = of(1);
ok; // 'OK'
const deprecated = of<number>(1);
deprecated; // 'deprecated' Thoughts? |
I'd prefer to leave it to a lint rule. Shenanigans with type parameters that don't directly relate to actual parameters makes me a little nervous. And the deprecation would be something that you'd want for all signatures except for the rest parameter one, so I guess that would involve double the number of type parameters for each of the non-rest-parameter signatures. |
Done, we should probably have a story around moving folks away from |
Sure. I have an item in my TODO list - from a discussion with @jwo719 - about creating an issue to deal with brining the RxJS linting rules into this repo. I'll make a note that another issue - relating to recommended rules, etc. - should reference this issue. |
Remove deprecation warning for valid use of
of