Skip to content

Instantly share code, notes, and snippets.

@hax
Last active September 24, 2024 05:32
Show Gist options
  • Save hax/d098906e80ddcf1cb76281d243b7ebf4 to your computer and use it in GitHub Desktop.
Save hax/d098906e80ddcf1cb76281d243b7ebf4 to your computer and use it in GitHub Desktop.
Verify Type Challenge
// example.js -- 示例
// verify.js -- 跑一下 type challenge
// play/tsconfig.json
// play/utils.d.ts
// 需要安装 typescript
// $ node example
// { exitCode: 0, stdout: '', stderr: '', error: undefined }
// {
// exitCode: 2,
// stdout: "code.ts(41,10): error TS2344: Type 'false' does not satisfy the constraint 'true'.\n" +
// "code.ts(42,10): error TS2344: Type 'false' does not satisfy the constraint 'true'.\n" +
// "code.ts(43,3): error TS2578: Unused '@ts-expect-error' directive.\n",
// stderr: '',
// error: undefined
// }
const { verify } = require("./verify")
const result1 = verify("console.log('Hello, world!')")
console.log(result1)
const example = `
/*
4 - Pick
-------
by Anthony Fu (@antfu) #easy #union #built-in
### Question
Implement the built-in \`Pick<T, K>\` generic without using it.
Constructs a type by picking the set of properties \`K\` from \`T\`
For example:
\`\`\`ts
interface Todo {
title: string
description: string
completed: boolean
}
type TodoPreview = MyPick<Todo, 'title' | 'completed'>
const todo: TodoPreview = {
title: 'Clean room',
completed: false,
}
\`\`\`
> View on GitHub: https://tsch.js.org/4
*/
/* _____________ Your Code Here _____________ */
type MyPick<T, K> = any
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<Expected1, MyPick<Todo, 'title'>>>,
Expect<Equal<Expected2, MyPick<Todo, 'title' | 'completed'>>>,
// @ts-expect-error
MyPick<Todo, 'title' | 'completed' | 'invalid'>,
]
interface Todo {
title: string
description: string
completed: boolean
}
interface Expected1 {
title: string
}
interface Expected2 {
title: string
completed: boolean
}
/* _____________ Further Steps _____________ */
/*
> Share your solutions: https://tsch.js.org/4/answer
> View solutions: https://tsch.js.org/4/solutions
> More Challenges: https://tsch.js.org
*/
`
const result2 = verify(example)
console.log(result2)
{
"compilerOptions": {
"target": "ESNext",
"module": "ESNext",
"moduleResolution": "node",
"allowJs": true,
"strict": true,
"noImplicitReturns": true,
"noUnusedLocals": false,
"skipLibCheck": true,
},
}
declare module "@type-challenges/utils" {
export type Expect<T extends true> = T
export type ExpectTrue<T extends true> = T
export type ExpectFalse<T extends false> = T
export type IsTrue<T extends true> = T
export type IsFalse<T extends false> = T
export type NotEqual<X, Y> = true extends Equal<X, Y> ? false : true
export type Equal<X, Y> =
(<T>() => T extends X ? 1 : 2) extends
(<T>() => T extends Y ? 1 : 2) ? true : false
// https://stackoverflow.com/questions/49927523/disallow-call-with-any/49928360#49928360
export type IsAny<T> = 0 extends (1 & T) ? true : false
export type NotAny<T> = true extends IsAny<T> ? false : true
export type Debug<T> = { [K in keyof T]: T[K] }
export type MergeInsertions<T> =
T extends object
? { [K in keyof T]: MergeInsertions<T[K]> }
: T
export type Alike<X, Y> = Equal<MergeInsertions<X>, MergeInsertions<Y>>
export type ExpectExtends<VALUE, EXPECTED> = EXPECTED extends VALUE ? true : false
export type ExpectValidArgs<FUNC extends (...args: any[]) => any, ARGS extends any[]> = ARGS extends Parameters<FUNC>
? true
: false
export type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never
}
const { spawnSync } = require("node:child_process")
const { writeFileSync, rmSync } = require("node:fs")
exports.verify = function verify(code) {
const file = `${__dirname}/play/code.ts`
writeFileSync(file, code)
try {
const result = spawnSync(`npx tsc --noEmit`, {
cwd: `${__dirname}/play`,
shell: true,
encoding: "utf-8",
timeout: 10_000,
})
return {
exitCode: result.status,
stdout: result.stdout,
stderr: result.stderr,
error: result.error,
}
} finally {
rmSync(file)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment