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
consttype parameters, so the floor is 5.3, and 7.0 is verified too. Projects onpreferences.jsonare 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'stypescriptwhen it has it, else the NativeScript CLI's own copy, else explains what to install. npx ns-preferences initcreatesapp/app.preferences.ts. Pass--jsonfor the previous behaviour;--typescript <file>now only applies with--json.
Added
definePreferences(definition): returnsPreferences<InferPreferences<typeof definition>>. Alistdefault that is not one of its options, amultilistdefault outside its options, a group inside a group, or a misspelled property is a compile error. The definition stays reachable assettings.definition.- Types:
PreferencesDefinition,PreferenceItemand the per-type item interfaces,InferPreferences<D>,PreferencePlatformOverride. - The hook and the CLI find
app.preferences.ts(or.js) in the app folder (appPathfromnativescript.config.ts, elsesrc, elseapp) or the project root, then fall back topreferences.json. Both present at once is an error. New CLI options--app-dirand--json. - Relative
.ts/.jshelper 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.jsonprojects: same schema, sameoutput.typescript, same generated module.- The
Preferencesclass,PreferencesView, the native stores, and every generated plist / XML byte.