Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Typescript can't type regconize error #59114

Closed
long-kr opened this issue Jul 2, 2024 · 5 comments
Closed

Typescript can't type regconize error #59114

long-kr opened this issue Jul 2, 2024 · 5 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@long-kr
Copy link

long-kr commented Jul 2, 2024

πŸ”Ž Search Terms

"Wrong array type"

πŸ•— Version & Regression Information

  • This is a dev time bug
  • Typescript version: 5.2.2
  • OS: Window
  • Editor: VSCode

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.2.2#code/FAAhGMHsDsGcBcQEt4FMC2sQF4QG1QwiBvQoogBwCdIATAV3HgH0laAuEAIjQQEYATAGYuAGjLkQANwCGVJDOgs2nHqn7CxE8gEd6i+CgCenQUPGTKNdJEMwACjQbgAkh2694ZrZYC+FogBdAG5gMKg4RAALRVoAG1QAQVpaFzRMABVIAGE5RFwZWCNocBAACgkUDFhOUktqOkZldwR5aABzUMtZeQNWFvg2zu0QPQNjTmh6dAAjVCouyQabO2hmBudmgH5OVqQOxbBfPECAsEIAShwAPhBiXzDgGOh4pJS06qzcqngyqswLkA

πŸ’» Code

// Your code here
 const items = [
    {
      product_id: productId,
      variant_id: variantId,
      quantity: submitQuantity,
      promotionProductId: promotionProductId,
    },
];
    
const handleAddItemsToCart = async (
    items: {
      product_id: string;
      variant_id: string;
      quantity: number;
      promotion_product_id?: string;
    }[] 
) => {}
  
handleAddItemsToCart(items)

πŸ™ Actual behavior

  • When I pass items as argument into handleAddItemsToCart function. Typescript doesn't show me that I promotionProductId is not a valid field in array item. If I change promotion_product_id?: string; to not nulable promotion_product_id: string;, typescript will show me that I'm passing wrong field.
  • I'm new to typescript, please correct me if I'm doing wrong here.

πŸ™‚ Expected behavior

  • Typescript will show error when passing item with field promotionProductId: promotionProductId.
@MartinJohns
Copy link
Contributor

MartinJohns commented Jul 2, 2024

There is no bug here. Additional properties are allowed to exist on objects, and promotion_product_id is optional and may be missing. This is working as intended.

See also the FAQ: https://github.com/microsoft/TypeScript/wiki/FAQ#indirect-excess-properties-are-ok

@long-kr
Copy link
Author

long-kr commented Jul 2, 2024

There is no bug here. Additional properties are allowed to exist on objects, and promotion_product_id is optional and may be missing. This is working as intended.

See also the FAQ: https://github.com/microsoft/TypeScript/wiki/FAQ#indirect-excess-properties-are-ok

I see, thank you for fast responding. Is there a way for me to strictly type for object type? @MartinJohns

@fatcerberus
Copy link

No, see #12936

@DanielRosenwasser DanielRosenwasser added the Working as Intended The behavior described is the intended behavior; this is not a bug label Jul 2, 2024
@long-kr
Copy link
Author

long-kr commented Jul 2, 2024

No, see #12936

Thank you! I was confusing a bit. Should I close the issue?

@typescript-bot
Copy link
Collaborator

This issue has been marked as "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Jul 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

5 participants