@nacrejs/types
v0.0.1
Published
Nacre 运行时的 TypeScript 类型定义
Readme
@nacrejs/types
TypeScript type definitions for Nacre, a JavaScript runtime for iOS apps.
This package has types only, no runtime code. The runtime puts its APIs on globalThis; this package declares them so the editor autocompletes them and the type checker catches mistakes.
Install
npm install -D @nacrejs/types typescriptAdd one line at the top of the entry file:
/// <reference types="@nacrejs/types" />tsconfig.json:
{
"compilerOptions": {
"target": "ESNext",
"lib": ["ESNext"],
"module": "Preserve",
"moduleResolution": "bundler",
"strict": true,
"noEmit": true,
"allowJs": true,
"checkJs": true,
"types": []
},
"include": ["src/**/*.js"]
}"lib": ["ESNext"]has noDOM: the runtime has nowindow,documentorlocation, and including DOM would make them look like they exist."allowJs"and"checkJs"type-check.jsfiles too, so the same setup works for JavaScript projects.
Effect: the editor autocompletes UIWindow, UILabel, application and the rest, and npx tsc --noEmit reports mistakes such as a misspelled property or an enum value that doesn't exist.
What's declared
| | Examples |
| --- | --- |
| Native classes | UIView, UILabel, UIColor, NSLayoutConstraint, with the same class names as in Apple's documentation |
| Nacre's own APIs | application, console, fetch, URL, timers |
Two rules carry native APIs over to JS:
// The method name is the first part of the selector; the other parts become keys of an options object
view.insertSubview(box, { atIndex: 0 }); // insertSubview:atIndex:
view.convertPoint(p, { toView: other }); // convertPoint:toView:
// Enum values are strings: the ObjC constant without its type prefix, starting lowercase
label.textAlignment = 'center'; // NSTextAlignmentCenterSee Native classes for all the rules, and the API reference for every class.
Capability packages such as @nacrejs/core-location ship their own types; add a /// <reference types="…" /> line for each one you install. See Core and capability packages.
License
FSL-1.1-MIT, see LICENSE.md.
