rhys-vdw / ts-auto-guard Goto Github PK
View Code? Open in Web Editor NEWGenerate type guard functions from TypeScript interfaces
License: MIT License
Generate type guard functions from TypeScript interfaces
License: MIT License
And correct "usage" in README
Once the tool is usable
I was getting some compilation errors with TypeScript 3.5.1 before I updated ts-simple-ast (ts-morph) version. Should it be done here perhaps?
Work out which types are required and import them... Need a way to check if they're global too:
Maybe this is just me but no matter what I do, I can't install the latest version of this package. npm install obviously won't work because the latest version is not published, but doing npm install with references to git repo usually works like a charm. I must've spent a couple hours on this and am completely baffled. Any ideas?
running ts-auto-guard
in cmdlet produces the following error:
(node:11252) UnhandledPromiseRejectionWarning: TypeError: realpath is not a function
at visitDirectory (C:\Users\admin\AppData\Roaming\npm\node_modules\ts-auto-guard\node_modules\typescript\lib\typescript.js:17838:45)
at Object.matchFiles (C:\Users\admin\AppData\Roaming\npm\node_modules\ts-auto-guard\node_modules\typescript\lib\typescript.js:17834:13)
at Object.matchFiles (C:\Users\admin\AppData\Roaming\npm\node_modules\ts-auto-guard\node_modules\ts-simple-ast\dist\typescript\tsInternal.js:7:35)
at Object.readDirectory (C:\Users\admin\AppData\Roaming\npm\node_modules\ts-auto-guard\node_modules\ts-simple-ast\dist\utils\compiler\readDirectory.js:16:27)
at Object.readDirectory (C:\Users\admin\AppData\Roaming\npm\node_modules\ts-auto-guard\node_modules\ts-simple-ast\dist\utils\tsconfig\getTsParseConfigHost.js:11:37)
at getFileNamesFromConfigSpecs (C:\Users\admin\AppData\Roaming\npm\node_modules\ts-auto-guard\node_modules\typescript\lib\typescript.js:28531:40)
at matchFileNames (C:\Users\admin\AppData\Roaming\npm\node_modules\ts-auto-guard\node_modules\typescript\lib\typescript.js:28451:16)
at getFileNames (C:\Users\admin\AppData\Roaming\npm\node_modules\ts-auto-guard\node_modules\typescript\lib\typescript.js:28000:26)
at parseJsonConfigFileContentWorker (C:\Users\admin\AppData\Roaming\npm\node_modules\ts-auto-guard\node_modules\typescript\lib\typescript.js:27932:18)
at Object.parseJsonConfigFileContent (C:\Users\admin\AppData\Roaming\npm\node_modules\ts-auto-guard\node_modules\typescript\lib\typescript.js:27879:16)
node version v10.15.3 on windows.
function isFoo(obj: any): obj is Foo {
if (process.env.DEBUG) {
return true
} else {
return (/* normal conditions here */)
}
}
Support some CLI option
$ ts-auto-guard --shortcircuit=process.env.DEBUG
This will be the same setup for anonymous object interface types
1.15.2
to 1.15.3
.This version is covered by your current version range and after updating it in your project the build failed.
prettier is a devDependency of this project. It might not break your production code or affect downstream projects, but probably breaks your build or test tools, which may prevent deploying or publishing.
There is a collection of frequently asked questions. If those don’t help, you can always ask the humans behind Greenkeeper.
Your Greenkeeper Bot 🌴
Preferably with a comment
// ts-auto-guard:generate
interface Foo {
Hey!
I just stumbled upon your project, great idea! I worked on some code generation tools for typescript in the past and I realized that a lot of logic can be extracted to another tool: things like globbing files, watching, prettifying generated code etc. I have work in progress version here: https://github.com/krzkaczor/ts-generator
Would you consider switching to ts-generator
in the future? Currently, I work on migrating https://github.com/krzkaczor/TypeChain to use ts-generator
. When it's done, its API should be more stable. I see that in your case finding source files to transpile is more complicating then just specifing glob pattern but I would be more than happy to tweak ts-generator API to match this use case.
What do you think?
interface Foo {
x: number,
}
/** @see {isFoo} ts-auto-guard:type-guard */
interface Foo {
y: number,
}
type X = { type: 'a', value: number } | { type: 'b', value: string }
function isX(obj: any): obj is X {
return (
(obj.type === 'a' && typeof obj.value === 'number') ||
(obj.type === 'b' && typeof obj.value === 'string')
)
}
Check interfaces that extend
others.
no import
prop: Enum | 5
import
prop: MyEnum
I noticed that the typeguards for these recursive types don't perform a useful test:
index.ts
:
// Recursion
/** @see {isRecursiveType} ts-auto-guard:type-guard */
export type RecursiveType = { children: RecursiveType[] };
// Mutual recursion
/** @see {isParentType} ts-auto-guard:type-guard */
export type ParentType = { type: 'parent', children: ChildType[] };
/** @see {isChildType} ts-auto-guard:type-guard */
export type ChildType = { type: 'child', parent: ParentType };
index.guard.ts
:
export function isRecursiveType(obj: any, _argumentName?: string): obj is RecursiveType {
return (
typeof obj === "object"
)
}
export function isParentType(obj: any, _argumentName?: string): obj is ParentType {
return (
typeof obj === "object"
)
}
export function isChildType(obj: any, _argumentName?: string): obj is ChildType {
return (
typeof obj === "object"
)
}
It feels safer when there is a link to repository in npm package page :)
I see that there’s a license referenced in the package.json, MIT, but this isn’t show in GitHub.
Please add a license file named ‘LICENSE’
Sent from PPHub
package.json contains some linux-specific code:
I am wondering if it could be improved somehow. I made some workarounds, but I am not very happy about them...
// room.ts
import { House } from "./house"
/** @see {isRoom} ts-auto-guard:type-guard */
interface Room {
width: number,
height: number,
}
// house.ts
/** @see {isHouse} ts-auto-guard:type-guard */
interface House {
address: string,
rooms: Room[]
}
// house.guard.ts
import { House } from "./house"
import { isRoom } from "./room.guard"
export function isHouse(obj: any): obj is House {
return (
typeof obj === "object" &&
typeof obj.address === "string" &&
Array.isArray(obj.rooms) &&
obj.rooms.every(isRoom)
)
}
typeof obj.prop === "function"
Failing test:
ts-auto-guard/tests/generate.ts
Lines 148 to 170 in 209fc67
Error: Node must be exported:
interface Bar {
bar: number
}
In this case the node does not need to be imported because it's in the same file, so no complaint is necessary.
Perhaps remove the error and just let the compiler inform the user that their code is broken.
Using node v12.18.2 or v13.13.0, I'm getting the follow error:
(node:47407) UnhandledPromiseRejectionWarning: TypeError: realpath is not a function
at visitDirectory (/Users/aneil/code/test-ts-auto-guard/node_modules/typescript/lib/typescript.js:18327:45)
at Object.matchFiles (/Users/aneil/code/test-ts-auto-guard/node_modules/typescript/lib/typescript.js:18323:13)
...
at Object.parseJsonConfigFileContent (/Users/aneil/code/test-ts-auto-guard/node_modules/typescript/lib/typescript.js:28544:16)
(node:47407) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 2)
Replicate with a folder containing the following files:
tsconfig.json
{
"compilerOptions": {
"baseUrl": "./src",
"outDir": "./lib",
},
"include": ["index.ts"],
"exclude": ["node_modules", "**/__tests__/*"]
}
index.ts
/** @see {isMyType} ts-auto-guard:type-guard */
type MyType = string | number;
package.json
{
"name": "test-ts-auto-guard",
"version": "1.0.0",
"main": "lib/index.js",
"types": "lib/index.d.ts",
"scripts": {
"build": "tsc"
},
"dependencies": {
},
"devDependencies": {
"ts-auto-guard": "^1.0.0-alpha.1"
}
}
I get the error with:
npm install
node_modules/.bin/ts-auto-guard
@keithlayne points out on Gitter:
@rhys-vdw I don’t know if you did this on purpose, but some of your nested objects can error on null values like so in your gist:
> a = null null > typeof a === 'object' && typeof a.b === 'string' TypeError: Cannot read property 'b' of null
e.g.
/** @see {isFoo} ts-auto-guard:type-guard */
interface Foo {
value: number
}
/** @see {isBar} ts-auto-guard:type-guard */
interface Bar {
foo: Readonly<Foo>
}
function isFoo(obj: any): obj is Foo {
return typeof obj === "object" && typeof obj.value === "number"
}
// EXPECTED:
function isBar(obj: any): obj is Bar {
return typeof obj === "object" && isFoo(obj.foo)
}
// ACTUAL:
function isBar(obj: any): obj is Bar {
return typeof obj === "object" && typeof obj.foo === "object" && typeof obj.foo.value === "number"
}
Warn that it's auto generated.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.