-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Conditional type on a lookup always never
#29211
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
I think that #29179 has broken this anyway since |
I think in my tinkering I've come up with variants that don't rely on such, so -- if there's a suitable alternative to make the example a bit more robust, let me know. 😄 I should add -- without a circular reference, I'm not sure what other kind of syntax I'd have to come up with to express what I want here. The type is effectively justifying itself, or bailing out. |
Yes, the code above now errors on the circular reference to |
@ahejlsberg - If that's the case, is there an alternate way for me to express:
I'm no fan of circular references, so I'm onboard there -- but I need a way to check if that type, when used as a key, retrieves a string. In this case, I'm not entirely convinced this is a circular reference, at least in intent. |
Not entirely clear on what you're trying to do, but I think you could write it like this: export interface PassthroughProps<
DataType extends {[key: string]: any},
DataTypeKey extends string = Extract<keyof DataType, string>,
IdentityCandidate = DataType[DataTypeKey] extends string ? DataType[DataTypeKey] : never,
> {
identityProperty: IdentityCandidate;
label: DataTypeKey | ((option: DataType) => string);
} However, it looks to me like the export interface PassthroughProps<
DataType extends {[key: string]: any},
DataTypeKey extends string = Extract<keyof DataType, string>
> {
identityProperty: DataType[DataTypeKey] extends string ? DataType[DataTypeKey] : never;
label: DataTypeKey | ((option: DataType) => string);
} Hope that helps. |
So, I think overall the advice here worked, but there's one thing that could possibly be improved: const key = item[this.props.identityProperty]; When I do the above, the value If I do this: const key = item[this.props.identityProperty!] as string; I get:
I can do that, but I feel like the compiler has enough information here to know that the value I get when using this key will have to be a string? |
This issue has been marked as 'Question' and has seen no recent activity. It has been automatically closed for house-keeping purposes. If you're still waiting on a response, questions are usually better suited to stackoverflow. |
TypeScript Version: 3.3.0-dev.20181229
Search Terms:
Code
Expected behavior:
Item 1
I'm expecting
identityProperty
to be( "id" | "name" )
, notnever
.Actual behavior:
Item 1
I'm getting this error:
The text was updated successfully, but these errors were encountered: