-
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
Destructured parameter should not enforce presence / non-nullability from type annotation on a property that's default-initialized #7678
Comments
the type inferred from I think what you are asking for is #7355 |
what is the proposal then? |
If you tell me what is unclear in the OP I can fix it.
|
but this is similar to: type RequiredParam = (a) => void;
var func: RequiredParam = (a?) => { }
func(); // Error |
And this issue is saying that it should, otherwise the user has to write verbose type aliases for each such use case.
No, this issue is only about destructured properties being optional, not the entire argument. |
I will bring this to discussion. |
Discussed for a while and decided this was just too confusing / complex (see #8228), with the acknowledgement that this is going to be annoying one way or another (i.e. making a new type or writing a big annotation or etc). Ultimately people (and the compiler) should be able to trust whatever type annotation is written as gospel. |
TypeScript Version:
55cbdc9 (current master)
Code
Expected behavior:
Component2({ propC: 0 })
andComponent2(s)
compile even though their parameters don't have propA and propB fromProps
, becauseComponent2
default-initializes those parameters.Actual behavior:
plus errors about
p
andComponent2(t)
which are expected.The reason to add the annotation on
Component2
is to get type information for the non-default-initialized propertypropC
.Is there any downside to allowing
Component2({ propC: 0 })
andComponent2(s)
?Component2(t)
should of course still not be allowed.s
and{ propC: 0 }
should still not be considered to beProps
for the purpose of type inference.Otherwise the user has to make a new type for each such function based on what's default-initialized and what isn't, like
type PropsForComponentFunction = { propA?: number, propB?: string, propC: number }
, and make sure the types match with the originalProps
. (Or inline the whole type declaration intoComponent2
but that has the same drawback.)The text was updated successfully, but these errors were encountered: