Blockdit Logo
Blockdit Logo (Mobile)
สำรวจ
ลงทุน
คำถาม
เข้าสู่ระบบ
มีบัญชีอยู่แล้ว?
เข้าสู่ระบบ
หรือ
ลงทะเบียน
Babel Coder
•
ติดตาม
29 ส.ค. 2022 เวลา 17:02 • วิทยาศาสตร์ & เทคโนโลยี
TypeScript 4.9 จะเข้ามากู้ Next.js ให้ Type Safety มากขึ้น
คืองี้นะ ปกติเราก็มีชนิดข้อมูลแบบ InferGetStaticPropsType หรือ InferGetServerSidePropsType ที่เอาไว้อนุมานชนิดข้อมูลของ props จาก getStaticProps / getServerSideProps เอาจริง ๆ แล้วมันก็อนุมานข้อมูลได้ถูกหละนะ
type Article = {
content: string
}
export const getStaticProps = async () => {
const res = await fetch('
https://www.babelcoder.com/articles
')
const articles: Article[] = await res.json()
return {
props: {
articles,
},
}
}
function BlogPage({ articles }: InferGetStaticPropsType) {
// articles จะมีชนิดข้อมูลเป็น Article[]
}
แต่ว่า ๆๆๆๆ มันก็มีบางเคสไงที่ให้ตายยังไงก็อนุมานผิดไปจากที่เราต้องการ เช่น
export const getStaticProps: GetStaticProps = async (context) => {
return {
props: {
id: 1,
title: "Hello Babel Coder"
}
}
}
const ArticlePage = (props: InferGetStaticPropsType) => {
// props มีชนิดข้อมูลเป็น { [key: string]: any; }
}
หลังจากที่ PR:
https://github.com/microsoft/TypeScript/pull/46827
merge เข้าไปแล้ว คาดว่า TypeScript 4.9 จะมี satisfies ให้ใช้งานแล้วแก้ปัญหาในส่วนนี้ได้ ตามภาพปลากรอบเลยครับ
นั่นไง TypeScript 4.8 พึ่งออก ของดีอยู่ 4.9 เลย อยู่มาแล้ว 8 ปี ออกซะทีอะไรดี ๆ ก็จะเกิดขึ้นหละนะ // หมายถึง TypeScript
ปอลิ่ง: /me ทุกวันนี้ใช้เป็นแค่ any จะเอาอะไรมา 4.9 ห๊ะ
บันทึก
โฆษณา
ดาวน์โหลดแอปพลิเคชัน
© 2025 Blockdit
เกี่ยวกับ
ช่วยเหลือ
คำถามที่พบบ่อย
นโยบายการโฆษณาและบูสต์โพสต์
นโยบายความเป็นส่วนตัว
แนวทางการใช้แบรนด์ Blockdit
Blockdit เพื่อธุรกิจ
ไทย