Add types to fetch,toast,bootstrap,svg (#31627)

Reduce `tsc` error count by 53. None of the changes has any runtime
effect.
This commit is contained in:
silverwind 2024-07-26 01:31:24 +02:00 committed by GitHub
parent cabcca3d81
commit 930ca92d7c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 56 additions and 33 deletions

View file

@ -146,17 +146,19 @@ const svgs = {
'octicon-x-circle-fill': octiconXCircleFill,
};
export type SvgName = keyof typeof svgs;
// TODO: use a more general approach to access SVG icons.
// At the moment, developers must check, pick and fill the names manually,
// most of the SVG icons in assets couldn't be used directly.
// retrieve an HTML string for given SVG icon name, size and additional classes
export function svg(name, size = 16, className = '') {
export function svg(name: SvgName, size = 16, className = '') {
if (!(name in svgs)) throw new Error(`Unknown SVG icon: ${name}`);
if (size === 16 && !className) return svgs[name];
const document = parseDom(svgs[name], 'image/svg+xml');
const svgNode = document.firstChild;
const svgNode = document.firstChild as SVGElement;
if (size !== 16) {
svgNode.setAttribute('width', String(size));
svgNode.setAttribute('height', String(size));
@ -165,7 +167,7 @@ export function svg(name, size = 16, className = '') {
return serializeXml(svgNode);
}
export function svgParseOuterInner(name) {
export function svgParseOuterInner(name: SvgName) {
const svgStr = svgs[name];
if (!svgStr) throw new Error(`Unknown SVG icon: ${name}`);
@ -179,7 +181,7 @@ export function svgParseOuterInner(name) {
const svgInnerHtml = svgStr.slice(p1 + 1, p2);
const svgOuterHtml = svgStr.slice(0, p1 + 1) + svgStr.slice(p2);
const svgDoc = parseDom(svgOuterHtml, 'image/svg+xml');
const svgOuter = svgDoc.firstChild;
const svgOuter = svgDoc.firstChild as SVGElement;
return {svgOuter, svgInnerHtml};
}