$ node <node-flag>#!/usr/bin/node | // my brain decided to ask the question: yknow, i want you to think about the y combinator -- | |
| // like, what is it. like what the fuck girl, cmon, you have a comp sci degree, you should know this, and understand it and shit | |
| // and so i was like fiiiiiiiiiiiiiiine gosh, lets see if typescript can handle the typing, and play around with it | |
| // so i looked up a javascript implementation, and played with the type defintion until it | |
| // matched up and then i was like oh: thats what the type definition of the functions in it are. | |
| // i get it now. that's pretty cool. the main interesting thing is a the inner function that takes itself | |
| // and returns the function initially passed to the outer function. neato. |
| import http from 'http'; | |
| import zlib from 'zlib'; | |
| const wait = (t = 100) => new Promise(r => setTimeout(r, t)); | |
| const STR = `<div>${'aaaaaaaaaa'.repeat(500)}</div>` | |
| const server = http.createServer(async (req, res) => { | |
| console.log('ON REQ'); | |
| if (req.url !== '/') return res.end(); |
| // golang 纯函数实现 | |
| package main | |
| import "fmt" | |
| type unknown = interface{} | |
| type Selector = func(unknown, unknown) unknown | |
| type Cons = func(Selector) unknown |
| type Fn = (...args: unknown[]) => unknown; | |
| // 取出 T 中静态 keys | |
| type StaticKeys<T> = { | |
| [K in keyof T]: T[K] extends Fn ? never : K | |
| }[keyof T] | |
| // 去除 T 里面的函数 | |
| type StaticObject<T> = Pick<T, StaticKeys<T>> |
class I1 { a: number = 11 };
class I2 { b: number = 11 };
type K1 = (k: I1) => void;
type K2 = (k: I2) => void;
type K1K2 = K1 | K2;| // TypeScript Version 4.3.5 | |
| // 原问题链接: | |
| // https://github.com/type-challenges/type-challenges/blob/master/questions/296-medium-permutation/README.md | |
| // 下面是思考过程: | |
| // 这个问题本质是如何穷举出形如 [1, 2, 3] 的所有组合, 那么如何告诉机器如何穷举呢 ? |
| type IsUnion<U> = ( | |
| // Union A | B 转 Intersection A & B (函参逆变) | |
| (U extends any ? ((args: U) => void) : never) extends ((args: infer R) => void) | |
| // 可理解为 [A | B] extends [A & B] ? false : true | |
| ? ([U] extends [R] ? false : true) | |
| : false | |
| ) | |
| type T1 = IsUnion<1>; // false | |
| type T4 = IsUnion<[1 | 2]>; // false |