-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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 to enforce "this interface A should be a descendent of an empty interface B"? #16451
Comments
Related: #202 |
also see #4895 |
@andy-ms are you refering to #202 (comment) ? In that case, the following snippet fails :( @Aleksey-Bykov but that doesn't work for interfaces / etc: declare class As<S extends string> {
private as: S;
}
type Basket = {
a?: number
} & As<'basket'>
const testBasket: Basket = {} // error: no 'as' in {} ps: same goes for #202 (comment) (the enum-version) |
#202 is the issue for nominal types, which seems to be what you want. I don't think there's any solution that would allow you to use an object literal as a nominal type without a cast. If you want to enforce that you only accept objects explicitly declared as an |
@devdoomari - as @Aleksey-Bykov pointed out in #202 (comment), but in case you didn't see it, all you need is a cast: declare class As<S extends string> {
private as: S;
}
type Basket = {
a?: number
} & As<'basket'>
// Casts are always necessary
const testBasket: Basket = {} as Basket |
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
I'm trying to enforce "IContext should be a descendent of IContextBase", even though IContextBase is empty.
With current set-up, I can only do:
but this method is quite cumbersome because I have to keep making variables with '__EXTENDS_IPROPS_BASE' in it:
Is there a better way?
ps: my question on stackoverflow might be relevant: https://stackoverflow.com/questions/44455020/tricking-typescript-compiler-to-assign-wrong-interface-to-a-variable
The text was updated successfully, but these errors were encountered: