-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
How --strictAny will support any in a contravariant position? #24711
Comments
FWIW you can always comment in closed issues; we do read/reply to those, especially design meeting notes. The PR notes mention special-casing Usually for |
For what it's worth, I don't think that |
Ok, thank you. I'll take note of that, I'm sorry 😊
Yes, you're right. But since there was suggested the possibility to remove that exception and encourage to use
I like that interpretation of |
Oh, well, I'll be expecting those details then. Was the main issue the proliferation of |
I think a lot of the conversation in the design meeting notes actually elaborates on it, but very offhandedly, I'd sum it up as
|
In addition to the above, I think the results of turning on the flag inside the compiler were informative. Our general policy in our codebase is never to use Turning the flag on found some assumptions in the tsconfig parser that no one has really noticed (i.e. it looks like you will crash the compiler if the top-level tsconfig content is the literal string I would honestly hate for this flag to be turned on automatically in a codebase I was maintaining - I just wouldn't expect it to yield any value; teams that are "careful" about not introducing |
@InExtremaRes Sorry for the silly question: when you say that I know you define type-safe when you say generic type-safe, but I'm not sure what you mean by generic. I feel this is obvious to others, I'm just not really sure I understand all the issues with |
Yes, basically. Maybe you are trying to constraint a generic to be an any function type, just like the Maybe you need to pass a printf-like function (don't know why): function setLogger(prinftLike: (m: string, ...args: any[]) => void) { /*...*/ } Perhaps you have a curry function that currifies the first argument; without variadic types this can be: function curry<U, R>(fn: (a1: U, ...args: any[]) => R, a: U) {
return (...b: any[]) => fn(a, ...b);
} Or just any time you need a function of any kind, even if you are not going to call it yourself. I am not saying these are good patterns or that it is the best way to do it, just giving examples of what I mean.
As you probably known, with declare let f1: (arg: T1) => void;
declare let f2: (arg: T2) => void;
f1 = f2; So for that last assignment to work If declare function bar(a1: string, b2: number): void;
function foo1(fn: (...args: any[]) => void) { /*...*/ }
function foo2(fn: (...args: {}[]) => void) { /*...*/ }
foo1(bar); // :)
foo2(bar) // type '{}' not assignable to type 'string' :C I am using the empty object type I am sorry if I'm over explaining here or I'm not answering your question at all. |
Everything is very clear, thank you! I appreciate the very detailed explanation. I now understand the problem. Where I was getting lost is that in your example: declare function bar(a1: string, b2: number): void;
function foo1(fn: (...args: any[]) => void) { /*...*/ }
function foo2(fn: (...args: {}[]) => void) { /*...*/ }
foo1(bar); // :)
foo2(bar) // type '{}' not assignable to type 'string' :C I think of the |
@DanielRosenwasser should we remove it from the 3.0 roadmap for now? Or are we still going to ship it there? |
I'm very sorry if this is not the place for this, but since #24593 is closed I am opening a new, more specific issue.
This is all related to #24423 (--strictAny).
In #24593 @DanielRosenwasser said:
For me this is a major concern since the pattern
(...args: any[]) => any
is now the best way to represent a generic function type.I suppose this can be a generic type-safe function (in the type-safe sense that neither arguments nor return can be used freely without an assertion of some kind):
Assuming an
unknown
type as described here #24439. Again as mentioned,never
seems very counterintuitive in this scenarios, the same forSemantically, that seems you telling "don't worry, type-checker, that value can never occur". I believe the weird part is the name
never
and not the semantics ofnever
itself.So I guess my questions are:
With the
--strictAny
flag enbled, which is the preferred idiom toany
in a contravariant position? Will benever
a more prominent type and suggested by the TypeScript team? Maybe there are plans to support a different pattern for this (should variadic types help here?)?Thank you very much in advance.
The text was updated successfully, but these errors were encountered: