Skip to content

Instantly share code, notes, and snippets.

View ksaldana1's full-sized avatar

Kevin Saldaña ksaldana1

View GitHub Profile
@ksaldana1
ksaldana1 / interfaces.types
Created June 25, 2018 22:47
#25157 Repro - File 1
// 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;
}
@ksaldana1
ksaldana1 / interfaces.ts
Last active June 25, 2018 23:15
#25157 Repro - File 1
// 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 };
@ksaldana1
ksaldana1 / route.ts
Last active June 25, 2018 22:59
#25157 Repro - File 2
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 };
}
{
"type": "Program",
"start": 0,
"end": 80,
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
@ksaldana1
ksaldana1 / example.ts
Last active January 30, 2019 04:01
ts-alias problem
/*
In node_module package 'my_company_protos'
export namespace protos {
export namespace user {
export interface User {
username: string;
info: protos.Info.User;
}
}
@ksaldana1
ksaldana1 / models.ts
Last active January 30, 2019 16:35
ts-alias-1
import { protos } from './my_company_protos'
export type User = protos.user.User;
@ksaldana1
ksaldana1 / number-transformer.ts
Last active January 30, 2019 05:18
number-transformer
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);
@ksaldana1
ksaldana1 / input-output.ts
Last active January 30, 2019 05:39
transform-input-output
// 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 {
@ksaldana1
ksaldana1 / aliasTransformer.ts
Last active May 6, 2019 07:20
aliasTransformer
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);
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
});
}