react-native typescrpit使用时的数据定义及扩展运算符报错问题记录

Function Components

type AppProps = {
  message: string;
}; 

const App = ({ message }: AppProps) => 
{message}
;

useState

const [count, setCount] = React.useState(0);

// later...
setUser(1);

useReducer

onst initialState = { count: 0 };

type ACTIONTYPE =
  | { type: "increment"; payload: number }
  | { type: "decrement"; payload: string };

function reducer(state: typeof initialState, action: ACTIONTYPE) {
  switch (action.type) {
    case "increment":
      return { count: state.count + action.payload };
    case "decrement":
      return { count: state.count - Number(action.payload) };
    default:
      throw new Error();
  }
}

function Counter() {
  const [state, dispatch] = React.useReducer(reducer, initialState);
  return (
    <>
      Count: {state.count}
      
      
    
  );
}

useRef

const ref1 = useRef(null!);
const ref2 = useRef(null);
const ref3 = useRef(null);

Class Components

type MyProps = {
  // using `interface` is also ok
  message: string;
};
type MyState = {
  count: number; // like this
};
class App extends React.Component {
  state: MyState = {
    // optional second annotation for better type inference
    count: 0,
  };
  render() {
    return (
      
{this.props.message} {this.state.count}
); } }

基本数据类型定义

type AppProps = {
  message: string;
  count: number;
  disabled: boolean;
  /** array of a type! */
  names: string[];
  /** string literals to specify exact string values, with a union type to join them together */
  status: "waiting" | "success";
  /** any object as long as you dont use its properties (NOT COMMON but useful as placeholder) */
  obj: object;
  obj2: {}; // almost the same as `object`, exactly the same as `Object`
  /** an object with any number of properties (PREFERRED) */
  obj3: {
    id: string;
    title: string;
  };
  /** array of objects! (common) */
  objArr: {
    id: string;
    title: string;
  }[];
  /** a dict object with any number of properties of the same type */
  dict1: {
    [key: string]: MyTypeHere;
  };
  dict2: Record; // equivalent to dict1
  /** any function as long as you don't invoke it (not recommended) */
  onSomething: Function;
  /** function that doesn't take or return anything (VERY COMMON) */
  onClick: () => void;
  /** function with named prop (VERY COMMON) */
  onChange: (id: number) => void;
  /** alternative function type syntax that takes an event (VERY COMMON) */
  onClick(event: React.MouseEvent): void;
  /** an optional prop (VERY COMMON!) */
  optional?: OptionalType;
};

React child 节点定义

interface AppProps {
  children1: JSX.Element; // bad, doesnt account for arrays
  children2: JSX.Element | JSX.Element[]; // meh, doesn't accept strings
  children3: React.ReactChildren; // despite the name, not at all an appropriate type; it is a utility
  children4: React.ReactChild[]; // better, accepts array children
  children: React.ReactNode; // best, accepts everything (see edge case below)
  functionChildren: (name: string) => React.ReactNode; // recommended function as a child render prop type
  style?: React.CSSProperties; // to pass through style props
  onChange?: React.FormEventHandler; // form events! the generic parameter is the type of event.target
  //  more info: https://react-typescript-cheatsheet.netlify.app/docs/advanced/patterns_by_usecase/#wrappingmirroring
  props: Props & React.ComponentPropsWithoutRef<"button">; // to impersonate all the props of a button element and explicitly not forwarding its ref
  props2: Props & React.ComponentPropsWithRef; // to impersonate all the props of MyButtonForwardedRef and explicitly forwarding its ref
}

使用中遇到的一些问题记录

interface  user{
  name:string,
  id:string
}
interface Appprops{
  userList:user[]
}
//此时如果使用 ...扩展运算符展开userList,ts会报错 说未实现Iterable接口
// eg  a= [...user] 
//改造user
interface  user{
  name:string,
  id:string,
  [Symbol.iterator]() : Iterator,
}
//如果想通过 user['name'] 的方式获取或设置user.name的值,需要再次改造user接口
interface  user{
  name:string,
  id:string,
  [Symbol.iterator]() : Iterator,
  [key:string]:string
}

你可能感兴趣的:(react-native typescrpit使用时的数据定义及扩展运算符报错问题记录)