Cannot change barNumberColor with NextJS and Typescript #2095
-
Hi! I'm trying to change barNumberColor, but keep getting type errors. const api = new AlphaTabApi(elementRef.current!, {
core: {
file: fileName,
fontDirectory: "/alphatab/font/",
},
player: {
enablePlayer: true,
enableCursor: true,
enableUserInteraction: true,
soundFont: "/alphatab/soundfont/sonivox.sf2",
},
display: {
staveProfile: StaveProfile.Tab,
resources: {
barNumberColor: '#FF0000',
}
},
} as Settings); But this did not work as barNumberColor requires a "Color" type, as per alphaTab.d.ts: /**
* The color to use for displaying the bar numbers above the music sheet.
* @defaultValue `rgb(200, 0, 0)`
* @since 0.9.6
*/
barNumberColor: Color; However, I cannot do something like |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
The second parameter of https://github.com/CoderLine/alphaTab/blob/develop/src/generated/SettingsJson.ts#L31 In your case the cast is the problem.
Beside that, alphaTab is currently shipped as one big bundle which makes importing sub-types a bit tricky. We plan to change that in 2.x. Talking code you can use one of the following flavors: Variant A: Object notation without cast. The IDE should detect that its the import { AlphaTabApi } as alphaTab from '@coderline/alphaTab';
const api = new AlphaTabApi(elementRef.current!, {
core: {
file: fileName,
fontDirectory: "/alphatab/font/",
},
player: {
enablePlayer: true,
enableCursor: true,
enableUserInteraction: true,
soundFont: "/alphatab/soundfont/sonivox.sf2",
},
display: {
staveProfile: StaveProfile.Tab,
resources: {
barNumberColor: '#FF0000',
}
},
}); Variant B: indicate that its the import * as alphaTab from '@coderline/alphaTab';
const api = new alphaTab.AlphaTabApi(elementRef.current!, {
core: {
file: fileName,
fontDirectory: "/alphatab/font/",
},
player: {
enablePlayer: true,
enableCursor: true,
enableUserInteraction: true,
soundFont: "/alphatab/soundfont/sonivox.sf2",
},
display: {
staveProfile: StaveProfile.Tab,
resources: {
barNumberColor: '#FF0000',
}
},
} satisfies alphaTab.json.SettingsJson); Variant C: import { AlphaTabApi, json } as alphaTab from '@coderline/alphaTab';
const api = new AlphaTabApi(elementRef.current!, {
core: {
file: fileName,
fontDirectory: "/alphatab/font/",
},
player: {
enablePlayer: true,
enableCursor: true,
enableUserInteraction: true,
soundFont: "/alphatab/soundfont/sonivox.sf2",
},
display: {
staveProfile: StaveProfile.Tab,
resources: {
barNumberColor: '#FF0000',
}
},
} satisfies json.SettingsJson); Variant C: construct the class instances and types import * as alphaTab from '@coderline/alphaTab';
const settings = new alphaTab.Settings();
settings.display.resources.barNumberColor = new alphaTab.model.Color(255, 0, 0);
settings.display.resources.barNumberColor = alphaTab.model.Color.fromJson('#FF0000')!;
const api = new alphaTab.AlphaTabApi(elementRef.current!, settings); Variant D: construct the class instances and types (dedicated imports) import { Settings, AlphaTabApi, model } as alphaTab from '@coderline/alphaTab';
const settings = new Settings();
settings.display.resources.barNumberColor = new model.Color(255, 0, 0);
settings.display.resources.barNumberColor = model.Color.fromJson('#FF0000')!;
const api = new AlphaTabApi(elementRef.current!, settings); |
Beta Was this translation helpful? Give feedback.
The second parameter of
AlphaTabApi
acceptsalphaTab.json.SettingsJson | alphaTab.Settings
.SettingsJson
is a more flexible format accepting the strict types but also some flavors like strings.https://github.com/CoderLine/alphaTab/blob/develop/src/generated/SettingsJson.ts#L31
https://github.com/CoderLine/alphaTab/blob/develop/src/generated/DisplaySettingsJson.ts#L157
https://github.com/CoderLine/alphaTab/blob/develop/src/generated/RenderingResourcesJson.ts#L110
https://github.com/CoderLine/alphaTab/blob/develop/src/model/Color.ts#L11
In your case the cast is the problem.
Settings
is really a class which has to be constructed, casting a plain object toSettings
is not correct.SettingsJson
…