This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Interface returned by our RouteCreator function | |
export interface Route<Parts extends PathPart<any>> { | |
template(): string; | |
create(params: UnionToIntersection<OnlyParams<Parts>>): string; | |
} | |
export interface PathParam<T extends string> { | |
param: T; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Interface returned by our RouteCreator function | |
export interface Route<Parts extends PathPart<any>> { | |
template(): string; | |
create(params: UnionToIntersection<OnlyParams<Parts>>): string; | |
} | |
export type PathPart<T extends string> = string | PathParam<T>; | |
export function param<T extends string>(t: T): PathParam<T> { | |
return { param: t }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { PathPart, Route } from './interfaces/types'; | |
export function isParam(i: any): i is PathParam<any> { | |
return i.param != null; | |
} | |
export function param<T extends string>(t: T): PathParam<T> { | |
return { param: t }; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"type": "Program", | |
"start": 0, | |
"end": 80, | |
"loc": { | |
"start": { | |
"line": 1, | |
"column": 0 | |
}, | |
"end": { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
In node_module package 'my_company_protos' | |
export namespace protos { | |
export namespace user { | |
export interface User { | |
username: string; | |
info: protos.Info.User; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { protos } from './my_company_protos' | |
export type User = protos.user.User; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const source = ` | |
const two = 2; | |
const four = 4; | |
`; | |
function numberTransformer<T extends ts.Node>(): ts.TransformerFactory<T> { | |
return context => { | |
const visit: ts.Visitor = node => { | |
if (ts.isNumericLiteral(node)) { | |
return ts.createStringLiteral(node.text); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// input | |
export namespace protos { // ModuleDeclaration | |
export namespace user { // ModuleDeclaration | |
// Module Block | |
export interface User { // InterfaceDeclaration | |
username: string; // username: string is PropertySignature | |
info: protos.Info.User; // TypeReference | |
} | |
} | |
export namespace Info { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import path from 'path'; | |
import ts from 'typescript'; | |
import _ from 'lodash'; | |
import fs from 'fs'; | |
const filePath = path.resolve(_.first(process.argv.slice(2))); | |
const program = ts.createProgram([filePath], {}); | |
const checker = program.getTypeChecker(); | |
const source = program.getSourceFile(filePath); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ts.forEachChild(source, node => { | |
if (ts.isTypeAliasDeclaration(node)) { | |
const symbol = checker.getSymbolAtLocation(node.name); | |
const type = checker.getDeclaredTypeOfSymbol(symbol); | |
const properties = checker.getPropertiesOfType(type); | |
properties.forEach(declaration => { | |
console.log(declaration.name); | |
// prints username, info | |
}); | |
} |
OlderNewer