Skip to content

Commit c87f823

Browse files
authored
fix: remove tsignore in LocalSvg and provide correct types (#1816)
PR adding proper typings for asset prop of LocalSvg based on Image.resolveAssetSource() typings. Based on #1593 by @pratyushok.
1 parent af68c93 commit c87f823

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

src/LocalSvg.tsx

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
11
import React, { useState, useEffect, Component } from 'react';
2-
import { NativeModules, Platform } from 'react-native';
3-
// @ts-ignore
4-
import resolveAssetSource from 'react-native/Libraries/Image/resolveAssetSource';
2+
import {
3+
NativeModules,
4+
Platform,
5+
Image,
6+
ImageSourcePropType,
7+
} from 'react-native';
58

69
import { fetchText } from './xml';
710
import { SvgCss, SvgWithCss } from './css';
811
import { SvgProps } from './elements/Svg';
912

1013
const { getRawResource } = NativeModules.RNSVGRenderableManager || {};
1114

12-
export function getUriFromSource(source?: string | number) {
13-
const resolvedAssetSource = resolveAssetSource(source);
15+
export function getUriFromSource(source: ImageSourcePropType) {
16+
const resolvedAssetSource = Image.resolveAssetSource(source);
1417
return resolvedAssetSource.uri;
1518
}
1619

17-
export function loadLocalRawResourceDefault(source?: string | number) {
20+
export function loadLocalRawResourceDefault(source: ImageSourcePropType) {
1821
const uri = getUriFromSource(source);
1922
return fetchText(uri);
2023
}
2124

22-
export function isUriAnAndroidResourceIdentifier(uri?: string | number) {
25+
export function isUriAnAndroidResourceIdentifier(uri?: string) {
2326
return typeof uri === 'string' && uri.indexOf('/') <= -1;
2427
}
2528

26-
export async function loadAndroidRawResource(uri?: string | number) {
29+
export async function loadAndroidRawResource(uri: string) {
2730
try {
2831
return await getRawResource(uri);
2932
} catch (e) {
@@ -35,7 +38,7 @@ export async function loadAndroidRawResource(uri?: string | number) {
3538
}
3639
}
3740

38-
export function loadLocalRawResourceAndroid(source?: string | number) {
41+
export function loadLocalRawResourceAndroid(source: ImageSourcePropType) {
3942
const uri = getUriFromSource(source);
4043
if (isUriAnAndroidResourceIdentifier(uri)) {
4144
return loadAndroidRawResource(uri);
@@ -50,7 +53,7 @@ export const loadLocalRawResource =
5053
: loadLocalRawResourceAndroid;
5154

5255
export type LocalProps = SvgProps & {
53-
asset?: string | number;
56+
asset: ImageSourcePropType;
5457
override?: Object;
5558
};
5659
export type LocalState = { xml: string | null };
@@ -69,13 +72,13 @@ export class WithLocalSvg extends Component<LocalProps, LocalState> {
6972
componentDidMount() {
7073
this.load(this.props.asset);
7174
}
72-
componentDidUpdate(prevProps: { asset?: string | number }) {
75+
componentDidUpdate(prevProps: { asset: ImageSourcePropType }) {
7376
const { asset } = this.props;
7477
if (asset !== prevProps.asset) {
7578
this.load(asset);
7679
}
7780
}
78-
async load(asset?: string | number) {
81+
async load(asset: ImageSourcePropType) {
7982
try {
8083
this.setState({ xml: asset ? await loadLocalRawResource(asset) : null });
8184
} catch (e) {

0 commit comments

Comments
 (0)