Edit, reasoning of the answer
Some of you can think this is not the answer for given question. But reality is that we as developers are responsible for modeling our data structures. Questioner asks here very specific question about how to model "{number}px" type in typescript. The answer is one - it is not possible. But the question is like that because questioner thinks this kind of type is a solution for his problem, what is misleading. The real problem is how to represent in type system the 2D point with two numeric values x,y and the unit. And this can be achieved by simple [number, number, 'px'], where we say we want two numbers and our unit is static 'px'. This kind of structure is flexible and type safe. Using some regexp, classes with setters and getters with throwing exception has nothing to compilation step, but is complex runtime validation.
Original answer:
Why you need a class for representing a pair. Using class for such simple construct is like killing a fly with a hammer. What you really need is simple pair + unit representation, which can be represented as tuple [a, b, unit] or record {a:a, b:b, unit: unit}. As others also pointed, there is no possibility to define type which have number with 'px' suffix, but you can model this in many ways. Few propositions:
1.Proposition one - modeling with additional unit information
// modeling as 3-nd tuple
type Point = [number, number, string]
const point = [1,2,'px']
// modeling as key-value map
type Point = {x: number, y: number, unit: string}
const point = {x:1,y:2,unit:'px'}
2.Modeling with strict unit type.
If we are sure that there will be strict unit like 'px', it can be define in type. To not repeat myself, I will show examples only in key-value map types.
type Point = {x: number, y: number, unit: 'px'} // unit property can be only px
const point = {x:1,y:2,unit:'px'}
Also we can create point constructor in order to avoid putting px by hand:
const createPoint = (x: number, y: number):Point => ({x,y,unit:'px'});
const point = createPoint(1,2) // {x:1,y:2,unit:'px'}
3.Modeling as pair of strings but with constructor
We can leave the type as pair of string but generate this by number constructor
type Point = {x: string, y: string}
const createPoint = (x: number, y: number):Point => ({x: x + 'px', y: y + 'px'});
const point = createPoint(1,2) // {x:'1px',y:'2px'}
4.Modeling as an object with special get functions
type Point = {values: () => {x: number, y: number}, pixels: () => {x: string, y: string}}
// below is using closure and stored in it arguments of createPoint function
const createPoint = (x:number, y:number): Point => ({
values: () => ({x, y}),
pixels: () => ({x: x + 'px', y: y + 'px'})
})
const point = createPoint(1,2);
point.values() // {x: 1, y: 2}
point.pixels() // {x: '1px', y: '2px'}