Skip to content

v2.1.0

Latest

Choose a tag to compare

@github-actions github-actions released this 09 Sep 01:02

Settings are now declared in TypeScript. app/app.preferences.ts exports definePreferences({ items }); keys, value types and option literals are inferred from the literal, and the same file is the runtime instance, so there is no generated settings.generated.ts any more. The build hook evaluates the file under Node, the way the NativeScript CLI reads nativescript.config.ts, and writes Settings.bundle and preferences.xml from it exactly as before. Suggested by the NativeScript core team on the RFC: NativeScript/rfcs#54 (comment)

Requirements

  • Requires TypeScript 6, the version NativeScript 9 ships with; the typings use const type parameters, so the floor is 5.3, and 7.0 is verified too. Projects on preferences.json are affected, since the typings are shared. Reading the definition at build time needs the TypeScript compiler API, which 7.x dropped: the hook uses the project's typescript when it has it, else the NativeScript CLI's own copy, else explains what to install.
  • npx ns-preferences init creates app/app.preferences.ts. Pass --json for the previous behaviour; --typescript <file> now only applies with --json.

Added

  • definePreferences(definition): returns Preferences<InferPreferences<typeof definition>>. A list default that is not one of its options, a multilist default outside its options, a group inside a group, or a misspelled property is a compile error. The definition stays reachable as settings.definition.
  • Types: PreferencesDefinition, PreferenceItem and the per-type item interfaces, InferPreferences<D>, PreferencePlatformOverride.
  • The hook and the CLI find app.preferences.ts (or .js) in the app folder (appPath from nativescript.config.ts, else src, else app) or the project root, then fall back to preferences.json. Both present at once is an error. New CLI options --app-dir and --json.
  • Relative .ts / .js helper imports work inside the definition. Any other import is rejected with a message, since the file runs in Node at build time.
  • Generated files name the definition they came from in their header.

Unchanged

  • preferences.json projects: same schema, same output.typescript, same generated module.
  • The Preferences class, PreferencesView, the native stores, and every generated plist / XML byte.