|
| 1 | +# @nativescript-dom/type-gen |
| 2 | + |
| 3 | +Generate framework-friendly types for NativeScript views from source code. This CLI scans NativeScript libraries (including your app) and produces: |
| 4 | + |
| 5 | +- React/Solid/Svelte/Vue: a single .d.ts “JSX runtime” file exposing NativeScript views as strongly typed intrinsic elements. |
| 6 | +- Angular: VS Code HTML Custom Data (metadata.json) and JetBrains Web Types (web-types.json) for template IntelliSense. |
| 7 | + |
| 8 | +It works by analyzing TypeScript sources using web-component-analyzer with a NativeScript-specific flavor. Views are discovered via JSDoc markers or, optionally, a legacy mode. |
| 9 | + |
| 10 | +## Why use it? |
| 11 | + |
| 12 | +- First-class IntelliSense on view attributes and events in templates/JSX. |
| 13 | +- Per-framework event naming and attribute casing out of the box. |
| 14 | +- Works for @nativescript/core and third‑party plugins. |
| 15 | +- Incremental generation with a dependency lock to avoid regenerating unchanged libraries. |
| 16 | + |
| 17 | +## Quick start |
| 18 | + |
| 19 | +The simplest way to use type-gen is via npx. You can run type-gen once using the `--all` flag to generate types for all your dependencies, or run it multiple times to generate core and plugin types separately. |
| 20 | + |
| 21 | +1. Generate framework “core” types once per project |
| 22 | + |
| 23 | +```bash |
| 24 | +npx @nativescript-dom/type-gen --core --framework <framework> |
| 25 | +``` |
| 26 | + |
| 27 | +2. Generate types for dependencies you use (plugins) |
| 28 | + |
| 29 | +```bash |
| 30 | +npx @nativescript-dom/type-gen --package @nativescript-community/ui-collectionview --framework <framework> --output types |
| 31 | +``` |
| 32 | + |
| 33 | +3. (Optional) Generate types for all dependencies listed in your package.json |
| 34 | + |
| 35 | +```bash |
| 36 | +npx @nativescript-dom/type-gen --all --framework <framework> --output types |
| 37 | +``` |
| 38 | + |
| 39 | +Where files go |
| 40 | + |
| 41 | +- By default, output is written to ./types. Use --output to change the folder. |
| 42 | + |
| 43 | +## CLI reference |
| 44 | + |
| 45 | +Every flag has a short and long form. You can also pass values as --flag=value. |
| 46 | + |
| 47 | +- -p, --package <name> |
| 48 | + - Generate types for a specific installed package (e.g. @nativescript-community/ui-collectionview). |
| 49 | + - The package must be resolvable from your project (node_modules). |
| 50 | + |
| 51 | +- -o, --output <dir> |
| 52 | + - Directory to write output files into. Default: ./types (created if missing). |
| 53 | + |
| 54 | +- -f, --framework <react|solid|svelte|vue|angular> |
| 55 | + - Target framework. Required for all commands that produce output. |
| 56 | + - Important: Use solid (not solidjs). |
| 57 | + |
| 58 | +- -c, --core |
| 59 | + - Generate the framework’s “core” types for @nativescript/core. |
| 60 | + |
| 61 | +- -a, --all |
| 62 | + - Generate for all dependencies listed in your project’s package.json. |
| 63 | + - Uses a lock file to skip unchanged entries between runs (see --reset). |
| 64 | + |
| 65 | +- -d, --directory <path> |
| 66 | + - Generate from a local directory of sources (not a package). Use when developing a library locally. |
| 67 | + |
| 68 | +- -r, --reset |
| 69 | + - Reset the dependency lock (forces --all to regenerate everything). |
| 70 | + |
| 71 | +- -n, --filename <name> |
| 72 | + - Override the output file name. Helpful for “core” files (e.g. core_react_jsx.d.ts). |
| 73 | + |
| 74 | +- -l, --legacy |
| 75 | + - Legacy discovery mode. Scans classes/interfaces that extend ViewBase and infers events (e.g. textChangeEvent -> TextChangeEventData). Prefer the JSDoc markers below for accuracy. |
| 76 | + |
| 77 | +- -h, --help |
| 78 | + - Print usage. |
| 79 | + |
| 80 | +### Lock file |
| 81 | + |
| 82 | +- Path: node_modules/.ns-type-gen/.ns-type-gen.lock.json |
| 83 | +- Written automatically on `--all` runs. Stores dependency versions to know what changed next time. |
| 84 | + |
| 85 | +## How views are discovered (for library authors) |
| 86 | + |
| 87 | +The CLI scans TypeScript source files to find NativeScript views and their properties/events based on the following JSDoc markers. |
| 88 | + |
| 89 | +- @nsView on the class or interface that represents a NativeScript view. |
| 90 | +- @nsProperty on public properties that should appear as attributes. |
| 91 | +- @nsEvent name EventType on event fields to expose them as events. |
| 92 | + |
| 93 | +Example View that will be discovered: |
| 94 | + |
| 95 | +```ts |
| 96 | +class ClickEventData extends EventData { |
| 97 | + // Must extend EventData |
| 98 | + public x: number; |
| 99 | + public y: number; |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * @nsView PluginView |
| 104 | + */ |
| 105 | +class ClickView extends View { |
| 106 | + /** |
| 107 | + * |
| 108 | + * @nsProperty |
| 109 | + */ |
| 110 | + public disable: string; |
| 111 | + |
| 112 | + /** |
| 113 | + * |
| 114 | + * @nsEvent {ClickEventData} click |
| 115 | + */ |
| 116 | + public clickEvent: string; |
| 117 | +} |
| 118 | +``` |
| 119 | + |
| 120 | +Gesture events |
| 121 | + |
| 122 | +- Common gesture events (tap, doubleTap, pan, swipe, rotation, longPress, touch) are added automatically to all views. |
| 123 | + |
| 124 | +Legacy mode |
| 125 | + |
| 126 | +- If you can’t add JSDoc, use --legacy. The analyzer will detect classes extending ViewBase and infer events with a ...Event string field. This is best‑effort, may be incomplete. |
| 127 | + |
| 128 | +## Framework setup guides |
| 129 | + |
| 130 | +Below are minimal steps distilled from the demo projects in [demos](demos/). Each demo contains a working setup you can copy. |
| 131 | + |
| 132 | +### React |
| 133 | + |
| 134 | +```json |
| 135 | +{ |
| 136 | + "compilerOptions": { |
| 137 | + "paths": { |
| 138 | + "ns-react/jsx-runtime": ["./types/core_react_jsx.d.ts"], |
| 139 | + }, |
| 140 | + }, |
| 141 | +} |
| 142 | +``` |
| 143 | + |
| 144 | +### Solid |
| 145 | + |
| 146 | +```json |
| 147 | +{ |
| 148 | + "compilerOptions": { |
| 149 | + "jsx": "preserve", |
| 150 | + "jsxImportSource": "ns-solid", |
| 151 | + "paths": { |
| 152 | + "ns-solid/jsx-runtime": ["./types/core_solid_jsx.d.ts"], |
| 153 | + }, |
| 154 | + }, |
| 155 | +} |
| 156 | +``` |
| 157 | + |
| 158 | +### Svelte |
| 159 | + |
| 160 | +```json |
| 161 | +{ |
| 162 | + "compilerOptions": { |
| 163 | + "jsxFactory": "svelteNS.JSX", |
| 164 | + "paths": { |
| 165 | + "ns-svelte/jsx-runtime": ["./types/core_svelte_jsx.d.ts"], |
| 166 | + }, |
| 167 | + }, |
| 168 | + "svelteOptions": { |
| 169 | + "namespace": "svelteNS.JSX", |
| 170 | + }, |
| 171 | +} |
| 172 | +``` |
| 173 | + |
| 174 | +### Vue (NativeScript-Vue 3) |
| 175 | + |
| 176 | +```json |
| 177 | +{ |
| 178 | + "compilerOptions": { |
| 179 | + "paths": { |
| 180 | + "ns-vue/jsx-runtime": ["./types/core_vue_jsx.d.ts"], |
| 181 | + }, |
| 182 | + }, |
| 183 | +} |
| 184 | +``` |
| 185 | + |
| 186 | +### Angular |
| 187 | + |
| 188 | +In .vscode/settings.json: |
| 189 | + |
| 190 | +```json |
| 191 | +{ |
| 192 | + "html.customData": ["./types/core_angular_metadata.json"], |
| 193 | +} |
| 194 | +``` |
| 195 | + |
| 196 | +You can add plugin metadata.json files here as well. |
| 197 | + |
| 198 | +## License |
| 199 | + |
| 200 | +MIT |
0 commit comments