Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8778025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T19:25:27+00:00 2026-06-13T19:25:27+00:00

var opts: {n?: number; s?: string;}; opts = {n: 2}; // Ok opts =

  • 0
var opts: {n?: number; s?: string;};
opts = {n: 2};     // Ok
opts = {s: 'x'};   // Ok
opts = {};         // Ok
opts = {z: 3};     // Ok, but shouldn't this be an error?

I want the opts object to accept none or any of the declared
optional properties (but no others), but it accepts other undefined
properties i.e. the declaration is equivalent to opts: {};, is this
a bug?

If it’s not a bug how can you construct such a declaration?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T19:25:29+00:00Added an answer on June 13, 2026 at 7:25 pm

    Part of the beauty of TypeScript is that it is quite clever at deciding whether an object can be coerced into another type.

    You declaration for opts actually has no requirements because both properties are optional, so any any object is compatible with this type. To be compatible, an object must contain all required properties – and in this case there are no required properties.

    This is clearer if we look at an adjusted example that does require a property:

    var opts: {a: bool; n?: number; s?: string;};
    
    opts = {a: true, n: 2};     // Ok
    opts = {a: true, s: 'x'};   // Ok
    opts = {a: true};         // Ok
    opts = {a: true,z: 3};     // Ok
    opts = {}; // not ok
    opts = {z: 3}; // not ok
    

    So this means an object is considered a sub-type of another object if it supports at least the required properties.

    That deals with setting the object. Now if you consider using the object you’ll see where the power lies. Because the type promises only n and s (and a in my example), the code calling properties on n can only ask for these properties. The additional properties cannot be accessed:

    var opts: {a: bool; n?: number; s?: string;};
    
    opts = {a: true, n: 1, s: 'Hi', z: 3};
    
    var x - opts.z; // not ok
    

    So we still have type safety, because we can only use the properties in the type declaration – not the additional properties.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want define defaults for my plugin, this is javascript code: var opts =
http://jsfiddle.net/CsrrD/ Given an object var viewModel = { Opts: ko.observableArray([ { d: 'a', v:
I want to call this function (makes an element to blink), declared as a
I have a very simple request, but warn me Parse Error : var http
var gethtml = $(#topmenu > li.l89 a).text().split(' ')[1].replaceWith('Any World'); The text is not changing
var fruit = [apple,pear,pear,pear,banana]; How do I remove all pear fruit from this array?
To be Google friendly, I want to change this script a little bit. I
I'm using node.js but I have a feeling this isn't necessarily related to just
My plugin is : $.fn.iPopup = function() { var opts = new function() {
This is my slideshow.js ; (function ($) { use strict; var ver = 'Lite-1.6';

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.