Skip to content

Instantly share code, notes, and snippets.

type NarrowingFilter<T, U extends T> = (x: T) => x is U;
function composeFilters<A, B extends A>(f1: NarrowingFilter<A, B>): NarrowingFilter<A, B>;
function composeFilters<A, B extends A, C extends B>(
f1: NarrowingFilter<A, B>,
f2: NarrowingFilter<B, C>,
): NarrowingFilter<A, C>;
function composeFilters<A, B extends A, C extends B, D extends C>(
f1: NarrowingFilter<A, B>,
f2: NarrowingFilter<B, C>,
f3: NarrowingFilter<C, D>,
use std::{rc::Rc, sync::Mutex};
use rand::Rng;
use wasm_bindgen::prelude::*;
fn draw_triangle(context: &web_sys::CanvasRenderingContext2d, points: [(f64, f64); 3], color: (u8, u8, u8)) {
let [top, left, right] = points;
let color_str = format!("rgb({},{},{})", color.0, color.1, color.2);
context.set_fill_style(&wasm_bindgen::JsValue::from_str(&color_str));
context.move_to(top.0, top.1);
@mizchi
mizchi / index.ts
Created July 19, 2023 12:01
sized-worker.
import * as ts from "typescript";
export interface Env {}
export default {
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
const num = ts.SyntaxKind.Identifier;
// const num = 80;
return new Response(`${num}`);
},
};
@mizchi
mizchi / package.json
Created July 14, 2023 10:32
Run RSC without next
{
"private": true,
"type": "module",
"scripts": {
"start": "tsm --conditions react-server run-rsc.tsx"
},
"license": "MIT",
"devDependencies": {
"@types/react": "^18.0.25",
"@types/react-dom": "^18.0.9",
/*
Prompt1:
この topoSort関数を以下の Graph 型に対するトポロジカルソートとして実装してください。
```ts
type Graph<T> = Map<T, Set<T>>;
export function topoSort<T>(graph: Graph<T>): Array<T> {
// wip
/*
# Benchmark for typescript compiler API - parse/print/transform
CAUTION: JIT is not taken into account
I use https://github.com/mizchi/svelte2tsx-component/blob/main/src/core.mts
---
code-size 43377chars 5336nodes
@mizchi
mizchi / InMemoryLanguageServiceHost.ts
Created June 12, 2023 13:50
TypeScript Language Manager in memory cache
import ts from "typescript/lib/tsserverlibrary.js";
import fs from "node:fs";
import path from "node:path";
import { DocumentRegistry } from "typescript";
const tsconfig = ts.readConfigFile("./tsconfig.json", ts.sys.readFile);
const options = ts.parseJsonConfigFileContent(tsconfig.config, ts.sys, "./");
const defaultHost = ts.createCompilerHost(options.options);
const expandPath = (fname: string) => {
// lightweight zod
type Validator<Expect> = (input: any) => input is Expect;
type ValidatorObject<Expect extends {}> = (input: any) => input is {
[K in keyof Expect]: Expect[K] extends Validator<infer CT> ? CT : never;
};
type ValidatorsToUnion<Vs> = Vs extends Array<Validator<infer T>> ? T
: never;
type EnumsToUnion<Vs> = Vs extends Array<Validator<infer T>> ? T
export interface Env {}
// very easy pattren only for this case
const path$join = (...paths: string[]) => {
return paths
.map(x => x.replace(/^\.\//, ''))
.join("/").replace(/\/+/g, "/");
}
const path$dirname = (path: string) => {
const parts = path.split("/");