You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a variety of string-based schemas (e.g. custom urls, ids of various kinds, etc.). I use the schemas for more than type validation, so I often access their properties (e.g. maxLength, pattern, etc.).
The issue I'm running in to is that TString seems to be entirely "open" as a type. TS will allow you to reference any property on a TString and it will assume it's an any. For example: mySchema.foozle = "this is going to fail at runtime since I don't have foozle defined";
I've tried to narrow the types by defining them like this:
But that doesn't work (it casts the optional away from maxLength but doesn't narrow the type further). Is there a way to have a "narrow" string schema that doesn't allow properties that aren't defined in string options (ideally it wouldn't allow properties that are not declared in the options that created the schema - i.e. no optional properties)
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I have a variety of string-based schemas (e.g. custom urls, ids of various kinds, etc.). I use the schemas for more than type validation, so I often access their properties (e.g. maxLength, pattern, etc.).
The issue I'm running in to is that
TString
seems to be entirely "open" as a type. TS will allow you to reference any property on aTString
and it will assume it's an any. For example:mySchema.foozle = "this is going to fail at runtime since I don't have foozle defined";
I've tried to narrow the types by defining them like this:
But that doesn't work (it casts the optional away from
maxLength
but doesn't narrow the type further). Is there a way to have a "narrow" string schema that doesn't allow properties that aren't defined in string options (ideally it wouldn't allow properties that are not declared in the options that created the schema - i.e. no optional properties)Beta Was this translation helpful? Give feedback.
All reactions