Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Argument of type 'string' is not assignable to parameter of type '{ results: string; }'.ts(2345)

When I’m trying to pass the result.nativeEvent.message to other function im getting the Argument of type ‘string’ is not assignable to parameter of type ‘{ results: string; } on onUnityMessageController(result.nativeEvent.message).

I don’t understand why the ‘string’ can’t be assigned to paramater of ‘string’.

Unity.tsx

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

import React, { useRef, useEffect } from 'react';
import UnityView from '@azesmway/react-native-unity';
import { View, Button, NativeSyntheticEvent } from 'react-native';
import { CommonActions } from '@react-navigation/native';

interface IMessage {
  gameObject: string;
  methodName: string;
  message: string;
}

const Unity = ({ navigation, route }: { navigation: undefined, route: any }) => {
  const unityRef = useRef<UnityView>(null);

  const shape = route.params.shape;
  const color = route.params.color;

  const parsedMess = JSON.stringify(route.params);
  console.log(parsedMess);

  const closeUnity = () => {
    unityRef.current?.unloadUnity();
    navigation.goBack();
  }

  useEffect(() => {
    if (unityRef?.current) {
      const message: IMessage = {
        gameObject: 'gameObject',
        methodName: 'methodName',
        message: 'message',
      };
      unityRef.current.postMessage(message.gameObject, message.methodName, message.message);
      if (shape != null && color != null) unityRef.current.postMessage("SceneManager", "startUnity", shape + ";" + color);
    }
  }, [shape, color]);



  const onUnityMessageController = ({ results }: { results: string }) => {
    console.log('onUnityMessage', results);
  }

  return (
    <View style={{ flex: 1 }}>
      <UnityView
        ref={unityRef}
        style={{ flex: 1 }}
        onUnityMessage={(result) => { onUnityMessageController(result.nativeEvent.message), console.log(result.nativeEvent.message) }} />
      <Button title="Close Unity Screen" onPress={() => closeUnity()} />
    </View >
  );
};

export default Unity;

result.nativeEvent.message comes from:

import React from 'react';
import { NativeSyntheticEvent, ViewStyle } from 'react-native';
interface UnityMessage {
    message: string;
}
declare type ReactNativeUnityViewProps = {
    androidKeepPlayerMounted?: boolean;
    fullScreen?: boolean;
    onUnityMessage?: (event: NativeSyntheticEvent<UnityMessage>) => void;
    onPlayerUnload?: (event: NativeSyntheticEvent<void>) => void;
    onPlayerQuit?: (event: NativeSyntheticEvent<void>) => void;
    style?: ViewStyle;
};
export default class UnityView extends React.Component<ReactNativeUnityViewProps> {
    static defaultProps: {};
    constructor(props: ReactNativeUnityViewProps);
    postMessage(gameObject: string, methodName: string, message: string): void;
    unloadUnity(): void;
    pauseUnity(pause: boolean): void;
    resumeUnity(): void;
    private getCommand;
    private getProps;
    componentWillUnmount(): void;
    render(): JSX.Element;
}
export { };

>Solution :

You are passing simple String as an argument to the function onUnityMessageController but your type check is expecting object.
So simply change your function likewise :

  const onUnityMessageController = (results : string) => {
    console.log('onUnityMessage', results);
  }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading