-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Optional object literal typing #1912
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
Comments
Right, casting is not the same because it would allow an object that is not necessarily an A. But you can do this: var a: { q: A } = {
q: {
x: 1,
y: "a"
}
} |
Jason's suggestion is the solution here. There's no good syntax for specifying the type on a per property basis, similar to trying to do the same inside a destructuring pattern. |
@danquirk it solves it but it requires to describe the whole structure of the object. |
If you use normal type annotation syntax then it's extremely awkward, if not actually ambiguous in some cases. If you don't, then you're inventing a new type annotation syntax for this one corner case. var a = {
q: A: { x: 1, y: 'a' }
} The vast majority of the time a cast at the property level suffices, or else you can use an annotation of the full object. |
An expression of the form |
Yes, to paraphrase @RyanCavanaugh's point, a type annotation var test: ITest = { foo: "foo" }; means that the object has to be an var test = <ITest>{foo: "foo" }; means that the object might be an |
Here the case where I need it:. This is declaration code interface B {
}
declare var B: {
prototype: B;
f(): B;
} And I want to define B. interface B {
}
declare var B: {
prototype: B;
}
var B = {
prototype:<B>null
} |
Ah, you are essentially splitting up the typing of B and the initialization of B into two steps. But you can also combine them: var B: {
prototype: B;
} = {
prototype: null
}; |
And if you want to do it in two steps, you can also do: var B: {
prototype: B;
};
B = {
prototype: null
}; |
I can but I already have a definition file that I cannot change since it comes from NuGet package. |
Then perhaps you just want B = {
prototype: null
} Note the absence of the word This does assume that a var B is already declared in the relevant scope. But if your definition file declares var B, it is implying that the definition is indeed in scope. |
The definition declares it but it is not defined in all environments . |
Ok, then I would go back to what I had said before: var B: {
prototype: B;
} = {
prototype: null
}; I agree this seems cumbersome, but I think the problem is that your definition file does not really reflect the environment of your unit test. Maybe the feature you want is something that merges declarations for vars, the same way declarations are merged for interfaces. If we had a feature like that, you could do var B = {
prototype: null
}; |
And to finish the thought, each initializer of the variable would have to have a type assignable to the aggregate type of all the type annotations of the declarations. |
And zero type annotations implicitly gives the variable type 'any'. |
Hmm, this looks like something entirely different. There is no property you could write there that has the semantics you want, you would have to assign it a function instead of an object. QQ = function() { }; Though in this case TypeScript would give you an error. When dealing with construct signatures, you're better off declaring a class, rather than an interface and a var. So instead, you might have: declare class QQ { } And now QQ will have the correct type. But you should only do this if you don't have to redeclare it later. So if your unittest has class QQ { } then don't include the |
Managed it. Thanks. |
Currently it is not possible to set a type for a key in the object literal.
However it is possible to cast a literal value but it is not the same.
The text was updated successfully, but these errors were encountered: