Skip to content

Instantly share code, notes, and snippets.

@wonderful-panda
wonderful-panda / test.ts
Last active February 22, 2018 05:47
Generate prop paths of nested module
// marker interface for module
interface Module {
_module_: 1
}
type ValueOf<T> = T[keyof T];
type PropPaths<
T,
K1 extends string = never,
K2 extends string = never,
@wonderful-panda
wonderful-panda / format.js
Created December 20, 2017 12:10
Vueファイルをフォーマットするやつ
/* Code formatter
* Based on prettier, and add support for Vue SFC
* For vue SFC, format only in <script> and <style>.
*
* Required dependencies
* - prettier
* - glob
* - chalk
*/
const prettier = require("prettier");
@wonderful-panda
wonderful-panda / run-prettier.js
Last active December 19, 2017 08:49
Use prettier with Vue single file component
// Example usage:
// node ./run-prettier.js "./**/*.{json,js,ts,vue}"
//
const prettier = require("prettier");
const glob = require("glob").sync;
const fs = require("fs");
const chalk = require("chalk");
/* RegExp to capture whole tag of script/style
*
@wonderful-panda
wonderful-panda / testComponent.ts
Created October 17, 2017 02:20
apply additional types to vue component (for vue 2.5)
import Vue, { VNode, VNodeChildrenArrayContents } from "vue";
import { VueConstructor } from "vue/types/vue";
export function vueWith<T>(): VueConstructor<Vue & T> {
return Vue as any;
}
export const TestComponent = vueWith<{
$refs: { test: HTMLDivElement },
$scopedSlots: {
type DiffKey<T extends string, U extends string> = (
& {[P in T]: P }
& {[P in U]: never }
& { [x: string]: never }
)[T];
type Omit<T, K extends keyof T> = Pick<T, DiffKey<keyof T, K>>;
type Diff<T, U> = Omit<T, keyof U & keyof T>;
@wonderful-panda
wonderful-panda / tsxsupport.ts
Created August 7, 2017 04:24
Vue component factory which supports type-safe JSX.
import Vue from "vue";
declare type Class<T> = new (...args: any[]) => T;
declare type EventHandlers<E> = {
[K in keyof E]?: (payload: E[K]) => void;
}
declare type ComponentOptions<Props> = Vue.ComponentOptions<Vue> & {
props: PropsDefinition<keyof Props>
};
@wonderful-panda
wonderful-panda / _runner.ts
Last active August 19, 2021 18:34
[TypeScript] 期待した通りのコンパイルエラーが出るかどうかテストするやつ
/*
* 1. ../tsconfig.json に設定ファイルを置く
* 2. ../cases/ の中に example.ts みたいなファイルを置く (/// TSXXXX: ~ でその行で期待するコンパイルを記述する。~の部分は正規表現)
* 3. _runner.ts を ava で実行すると、出力サンプルみたいな感じで結果が出力される
*/
import * as ts from "typescript/lib/typescript";
import * as fs from "fs";
import * as path from "path";
import * as glob from "glob";
@wonderful-panda
wonderful-panda / builder.ts
Last active March 18, 2018 19:22
vuex store builder (TypeScript >= 2.1 is required / namespaced module is not supported)
// MIT License
import * as Vuex from "vuex";
/*
* Infrastructure types
*/
export type KV<K extends string, V> = { [_ in K]: V };
export type Payload<T, V> = { type: T } & V;
/*
@wonderful-panda
wonderful-panda / example.ts
Last active January 13, 2017 03:54
keyof難しい その3
interface Context { prop1: string; }
type Handlers<T> = {
[K in keyof T]: (ctx: Context, payload: T[K]) => void
};
function test<T>(handlers: Handlers<T>): Partial<T> {
return {} as any;
}
const t = test({
foo(ctx, payload: number) {
@wonderful-panda
wonderful-panda / example.ts
Created December 16, 2016 03:42
Asynchronous test of vue with mocha
import * as Vue from "vue";
import * as assert from "power-assert";
function nextTick() {
return new Promise((resolve, _) => Vue.nextTick(resolve));
}
describe("vue component tests", function() {
it("works properly", async function() { // don't use `done` with async function
const vm = new Vue({ ... });