React: Support es6 symbols in proptype

Created on 20 Sep 2015  路  13Comments  路  Source: facebook/react

Would you be open to support es6 symbols in proptype?

At the moment I'm testing them as typeof Symbol() === 'symbol' in custom proptype functions.

starter

Most helpful comment

We鈥檙e still ironing out how we鈥檒l move on with cutting branches on 15.x but once it鈥檚 decided I expect this to get in.

All 13 comments

Here's what I use (not sure if it's the right approach):

function symbol(props, propName, componentName) {
    const prop = props[propName];
    if (prop && (typeof prop !== 'symbol')) {
        return new Error(`[${componentName}]: Expected property ${propName} to be a Symbol, but its actual type is ${typeof prop}.`);
    }
}

symbol.isRequired = function(props, propName, componentName) {
    return props[propName] ?
           symbol(props, propName, componentName) :
           new Error(`[${componentName}]: Property ${propName} is required.`);
};

export default symbol

We've generally said no to a lot of custom proptypes checks but this one feels like it's probably ok. In theory it's a 1 line change. However we aren't applying the Symbol transform (which changes all typeof checks) so we'd have to do something similar with the type (already have that in place for RegExps and old browsers) - not hard but just a little bit more work.

Thoughts @sebmarkbage @spicyj

Yea. Seems reasonable. The .constructor === Symbol as a fallback is ok. Not ideal since it doesn't work cross-realm but we should use whatever is standard practice among popular polyfills.

Any possibility that this will be added in future versions?

Yes, we just never got back to it. If you're interested in working on it, this is something that would make a nice addition in 15.1.

@zpao No one is assigned? I could do a PR for it!

GitHub doesn鈥檛 make it very obvious :cry:

Usually when people start working on a feature they reference it, and it appears in the issue.
You can see the existing PR above:

screen shot 2016-03-31 at 14 20 27

Also, you can't assign issues to people who don't have commit access, making it much harder to communicate that somebody outside the core team is working on it.

Any update on this? I suppose I'll switch to string constants in this one instance, but symbols would've been better!

@ffxsam A PR is running on #6377 -- I don't know what is the status, but it looks complete on our side. Maybe, it will be merged soon!

We鈥檙e still ironing out how we鈥檒l move on with cutting branches on 15.x but once it鈥檚 decided I expect this to get in.

Looks like #6377 was merged. Should be in 15.1 or 15.2.

Was this page helpful?
0 / 5 - 0 ratings