-
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
Error: Type cannot be used to index type after two indexes #21760
Comments
Likely related to #21368 |
@sandersn can you take a look. |
The nut of the problem is that the base constraint of I think it would be possible to fix this case by changing the rule for keyof types in |
Here's the change I made. It re-broke #15371, so I'm not sure it's the right change. I'll discuss with @ahejlsberg tomrrow. if (t.flags & TypeFlags.Index) {
- return stringType;
+ const baseType = getBaseConstraint((t as IndexType).type);
+ const baseIndex = baseType ? getIndexType(baseType) : undefined;
+ return baseIndex && baseIndex !== unknownType ? getBaseConstraint(baseIndex) : undefined;
} |
@sandersn hello, maybe there is any news? |
This is a 2.8 upgrade blocker for me @DanielRosenwasser |
Actually, it's really weird! I'm getting type errors, but TS is getting the derived type right anyway! |
Is there any chance to get it fixed? I've got it working in my case, but only with strictNullChecks: false. We absolutely love strictNullChecks, so waiting for the fix) |
So I have this piece of code: type D<T> = {
[propertyName: string]: keyof T;
}
const f = <A, B, C extends D<B>>(
a: A, b: B, c: C,
): (
A & {
[K in keyof C]: B[C[K]] // this line
}
) => void 0 as any;
const result = f({
abc: 1,
}, {
def: true
}, {
ghi: 'def'
}); In TS 3.0.3 I'd greatly appreciate any help. |
ERROR in node_modules/@amcharts/amcharts4/.internal/charts/types/XYChart.d.ts(258,33): error TS2344: Type 'this["_xAxisRendererType"]' does not satisfy the constraint 'AxisRenderer'. Please.... Help Me... |
Maybe related:
This shows the error: |
Maybe related: // This does NOT compile
export type Nested = {
nest: {
foo: string[];
bar: number[];
};
};
export const test = <
T extends keyof Nested,
K extends keyof Nested[T],
V extends Nested[T][K][number] //<-- Type 'number' cannot be used to index type 'Nested[T][K]'.
>(
type: T,
key: K,
value: V
) => {
console.log(type, key, value);
};
// desired interface:
test("nest", "foo", "value");
test("nest", "bar", 0); Specifically, I want to get the type of the members of the nested array. Note, this works fine in the non-nested case, e.g. // This compiles:
export type NonNested = {
foo: string[];
bar: number[];
};
export const test = <
T extends keyof NonNested,
V extends NonNested[T][number]
>(
type: T,
value: V
) => {
console.log(type, value);
};
test("foo", "value");
test("bar", 0); |
An additional test case from @bmeck (TS playground): const constRoutes = {
users: {
admin: {
get: '/admin'
}
}
} as const
type ConstRoutes = typeof constRoutes
type ConstRouteSectionName = keyof ConstRoutes
type ConstRouteModelName<Section extends ConstRouteSectionName> = keyof ConstRoutes[Section]
function getForConstRoute<
Section extends ConstRouteSectionName,
Model extends ConstRouteModelName<Section>
>(section: Section, model: Model): string {
// Cannot figure out that these are coming out of well known keys?
return constRoutes[section][model].get
}
// ensure error on wrong param
getForConstRoute('users', 'admin2') Although I think that perhaps this also suffers from lack of something like indexed access improvements for mapped types. It seems that TS could try to understand those kind of relationships better here - since those types are derived from other types. |
Unsure if this is related, but it seems so: interface IndexedActions {
a: {
start: {
foo: string;
}
}
}
type DeriveInputType<N extends keyof IndexedActions, A extends keyof IndexedActions[N]> =
IndexedActions[N][A] & { namespace: N; verb: A };
function doAction<N extends keyof IndexedActions, A extends keyof IndexedActions[N]>(action: DeriveInputType<N, A>) {
const s: string = action.verb;
// ^^^^ `string | number | symbol`, not `'start'`
} |
TypeScript Version: 2.7.1, 2.8.0-dev.20180208
Search Terms:
Type cannot be used to index type
Code
Expected behavior:
In version 2.7.0-dev.20171115 this code was checking without errors.
Actual behavior:
Now in throws error: Type '"baz"' cannot be used to index type 'IExample[name][val]'.
Playground Link:
https://www.typescriptlang.org/play/index.html#src=interface%20IExample%20%7B%0D%0A%20%20%20%20foo%3A%20%7B%0D%0A%20%20%20%20%20%20%20%20bar%3A%20%7B%0D%0A%20%20%20%20%20%20%20%20%20%20%20%20baz%3A%20number%3B%0D%0A%20%20%20%20%20%20%20%20%7D%0D%0A%20%20%20%20%7D%0D%0A%7D%0D%0A%0D%0Atype%20F%20%3D%20%3C%0D%0A%20%20%20%20name%20extends%20keyof%20IExample%2C%0D%0A%20%20%20%20val%20extends%20keyof%20IExample%5Bname%5D%0D%0A%3E()%20%3D%3E%20IExample%5Bname%5D%5Bval%5D%5B'baz'%5D%3B
The text was updated successfully, but these errors were encountered: