Typescript string type that must start with specific characters

As of TypeScript 4.1, you can use a template literal type for this. type StartsWithPrefix = `prefix${string}`; This works for type unions as well: // `abc${string}` | `def${string}` type UnionExample = `${‘abc’ | ‘def’}${string}`; // For your example: type MyPrefixTypes = `${MyStringTypes}${string}`; const ok: UnionExample=”abc123″; const alsoOk: UnionExample=”def123″; const notOk: UnionExample=”abdxyz”; // Note that a … Read more

What is the Pythonic Way of Differentiating Between a String and a List?

No need to import modules, isinstance(), str and unicode (versions before 3 — there’s no unicode in 3!) will do the job for you. Python 2.x: Python 2.6.1 (r261:67515, Feb 11 2010, 00:51:29) [GCC 4.2.1 (Apple Inc. build 5646)] on darwin Type “help”, “copyright”, “credits” or “license” for more information. >>> isinstance(u”, (str, unicode)) True … Read more

How to check a variable against Union type during runtime?

In Python 3.8 and later, the approach suggested by MSeifert and Richard Xia can be improved by not using the undocumented attributes __origin__ and __args__. This functionality is provided by the new functions typing.get_args(tp) and typing.get_origin(tp): >> from typing import Union, get_origin, get_args >> x = Union[int, str] >> get_origin(x), get_args(x) (typing.Union, (<class ‘int’>, <class … Read more

Python — Check if object is instance of any class from a certain module

You can use inspect.getmembers to get all the classes in your module: inspect.getmembers(my_module,inspect.isclass) This will return a list of name-class pairs. You just want the classes: my_module_classes = tuple(x[1] for x in inspect.getmembers(my_module,inspect.isclass)) One thing that I managed to overlook when I initially wrote this answer is the ability to check a class’s __module__ attribute. … Read more

Type checking in C++

There are two ways that you can do this. First, you can use the typeid operator, which returns a type_info structure containing information about the type of the object. For example: Base* ptr = /* … */ if (typeid(*ptr) == typeid(DerivedType)) { /* … ptr points to a DerivedType … */ } Notice that you … Read more

Try/catch or validation for speed?

I upvoted Matt Joiner’s answer, but wanted to include some additional observations to make it clear that, along with a couple of other factors, there are 4 times that matter when choosing between pre-checking conditions (known as LBYL or “Look Before You Leap”) and just handling exceptions (known as EAFP or “Easier to Ask Forgiveness … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)