0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

expo-routerとtsで、push()の型エラーが取れない際の解決方法

Posted at

環境

TypeScript expo-router

症状

上記環境で、router.push()を書いたとき、TSでエラーが発生します。 ざっくり「"/(profile)/profileEditScreen"」という文字をpush()には入れられませんよーって怒られてます

エラーを見ると、いくつか今まで動かしてたフォルダ名が並んでいるものの、引数に入れたパスもすでにフォルダ、ファイルとして存在しています。しかし、エラーが発生します

また、すでに消したフォルダパスが表示されています。

error
型 '"/(profile)/profileEditScreen"' の引数を型 '"/entryScreen" | "/loginScreen" | "/signUpScreen" | RelativePathString | ExternalPathString | "/_sitemap" | `/_sitemap?${string}` | `/_sitemap#${string}` | ... 50 more ... | { ...; }' のパラメーターに割り当てることはできません。ts(2345)

以下みたいな感じで書いてます。

hoge.ts
import { useRouter } from 'expo-router';

export default function HogeScreen() {
  const router = useRouter();
  
  function handleButton(){
      router.push("/(profile)/profileEditScreen")//エラーが出る
  }
}

解決方法

expo/types/router.d.tsファイルを削除して、npx expo start -cで再度ビルドすると現在のフォルダ構成通りexpo/types/router.d.tsが再生成されて、解決しました。

router.d.tsがパスを管理していて、実際にファイルを置いたらパスが通るわけじゃないようでした。
前回のビルド時のパスが残っていたら、新しくパスを書いたりファイルの場所を変更したりしても前回のパスを参照してTSがエラーを吐いてしまうようです。

npx expo start -c
//これでrouter.d.tsが再生成される
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?