Skip to content

Commit 2baee7c

Browse files
Fix #115: indexing type with generic
1 parent 59e67d5 commit 2baee7c

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

src/transform-inline/visitor-indexed-access.ts

+3
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ function visitObjectType(type: ts.ObjectType, indexType: ts.Type, visitorContext
8787
} else if (visitorContext.checker.getIndexTypeOfType(type, ts.IndexKind.Number)) {
8888
// Index type is number -> array type.
8989
return visitArrayObjectType(type, indexType, visitorContext);
90+
} else if ((ts.TypeFlags.TypeParameter & indexType.symbol?.flags) !== 0) {
91+
// Index type is type -> type
92+
return VisitorTypeCheck.visitType(indexType, visitorContext);
9093
} else {
9194
// Index type is string -> regular object type.
9295
return visitRegularObjectType(type, indexType, visitorContext);

test/issue-115.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import * as assert from 'assert';
2+
import { is } from '../index';
3+
4+
/* https://github.com/woutervh-/typescript-is/issues/115 */
5+
6+
export const Status = {
7+
enable: 'enable',
8+
disable: 'disable'
9+
} as const;
10+
11+
type StatusObject<Key extends keyof typeof Status> = {
12+
status: (typeof Status)[Key];
13+
};
14+
15+
describe('is', () => {
16+
describe('Accessing generic member of a type', () => {
17+
it('should return true for the right member', () => {
18+
assert.deepStrictEqual(is<StatusObject<'enable'>>({ status: Status.enable }), true);
19+
});
20+
it('should return false for the wrong member', () => {
21+
assert.deepStrictEqual(is<StatusObject<'enable'>>({ status: Status.disable }), false);
22+
});
23+
});
24+
});

0 commit comments

Comments
 (0)