react router 6 官方案例
// The `backgroundLocation` state is the location that we were at when one of
// the gallery links was clicked. If it's there, use it as the location for
// the <Routes> so we show the gallery in the background, behind the modal.
let state = location.state as { backgroundLocation?: Location };
location.state
是什么类型,这里看不出来。但是as
关键字后面接的是一个类型,即{ backgroundLocation? Location }
,如果用interface
或者type
定义类型的写法,会是这样:它定义了一个对象类型,该对象有
backgroundLocation
属性,该属性是可选的 (?
),是Location
类型。所以这句话的意思就是把
location
的state
属性引用的对象,看作 (treat as){ backgroundLocation?: Location}
这样一个类型,并使用state
这样一个局部变量来引用(赋值给他)。