diff --git a/src/transform-inline/visitor-utils.ts b/src/transform-inline/visitor-utils.ts index 8db6555..b8114ae 100644 --- a/src/transform-inline/visitor-utils.ts +++ b/src/transform-inline/visitor-utils.ts @@ -71,7 +71,14 @@ export function getPropertyInfo(parentType: ts.Type, symbol: ts.Symbol, visitorC let isFunction: boolean | undefined = undefined; let optional: boolean | undefined = undefined; - if ('valueDeclaration' in symbol) { + if ('type' in symbol) { + // Attempt to get it from 'type' + + propertyType = (symbol as { type?: ts.Type }).type; + isMethod = false; + isFunction = false; + optional = ((symbol as ts.Symbol).flags & ts.SymbolFlags.Optional) !== 0; + } else if ('valueDeclaration' in symbol) { // Attempt to get it from 'valueDeclaration' const valueDeclaration = symbol.valueDeclaration; @@ -94,13 +101,6 @@ export function getPropertyInfo(parentType: ts.Type, symbol: ts.Symbol, visitorC propertyType = visitorContext.checker.getTypeFromTypeNode(valueDeclaration.type); } optional = !!valueDeclaration.questionToken; - } else if ('type' in symbol) { - // Attempt to get it from 'type' - - propertyType = (symbol as { type?: ts.Type }).type; - isMethod = false; - isFunction = false; - optional = ((symbol as ts.Symbol).flags & ts.SymbolFlags.Optional) !== 0; } else if ('getTypeOfPropertyOfType' in visitorContext.checker) { // Attempt to get it from 'visitorContext.checker.getTypeOfPropertyOfType' diff --git a/test/issue-115.ts b/test/issue-115.ts new file mode 100644 index 0000000..65c36c5 --- /dev/null +++ b/test/issue-115.ts @@ -0,0 +1,37 @@ +import * as assert from 'assert'; +import { is } from '../index'; + +/* https://github.com/woutervh-/typescript-is/issues/115 */ + +export const Status = { + enable: 'enable', + disable: 'disable' +} as const; + +type StatusObject = { + status: (typeof Status)[Key]; +}; + +const testObj = { + outer: 0 +}; + +describe('is', () => { + describe('Accessing generic member of a type', () => { + it('should return true for the right member', () => { + assert.deepStrictEqual(is>({ status: Status.enable }), true); + }); + it('should return false for the wrong member', () => { + assert.deepStrictEqual(is>({ status: Status.disable }), false); + }); + }); + + describe('Using typeof instead of a type', () => { + it('should return true for the right member', () => { + assert.deepStrictEqual(is('test'), false); + }); + it('should return false for the wrong member', () => { + assert.deepStrictEqual(is({ outer: 5 }), true); + }); + }); +}); \ No newline at end of file