-
Notifications
You must be signed in to change notification settings - Fork 56
Settings
There are two ways to introduce type checking for your package's ClientSettings with the type definitions.
By defining your settings under the global namespace ClientSettings.Values
, game.settings.get
and set
can become typed correctly for your package.
Consider the following setting definition:
game.settings.register('gm-screen', 'right-margin', {
default: 4,
type: Number,
scope: 'world',
config: true,
});
In a global.d.ts
(or similar file where you're doing declaration merging), adding a declaration like the following will type the above setting correctly:
declare global {
namespace ClientSettings {
interface Values {
'gm-screen.right-margin': number;
}
}
}
Produces the correct type checks when registering, setting, and getting the setting.
Some tests here demonstrate the technique: https://github.com/League-of-Foundry-Developers/foundry-vtt-types/blob/main/test-d/foundry/common/abstract/document.mjs.test-d.ts#L44
ClientSettings.register
is a generic type and thus accepts parameters. This will allow setting registrations to work in some cases where they don't automatically (e.g. a Number with a range has issues inferring its types with a range
present at the time of this writing). To bypass this, the arguments for the generic can be passed directly.
game.settings.register<string, string, number>('some', 'numberSetting', {
default: 4,
type: Number,
scope: 'world',
range: { min: 0, max: 75, step: 5 },
config: true,
})
Note that if this is done, the setting's type is unknown
when get
ing it elsewhere. This is much less useful than properly merging your setting types with the method outlined above.