class Foo {
a!: number;
b!: number;
}
function bar(arg:Foo) {};
const randomObj = {a:1, b:2, c:5};
bar(randomObj); // ok
class A {
static bool: boolean = true;
}
class B extends A {
static readonly bool = true;
}
class C extends A {
static readonly bool = false;
}
function func<T extends typeof A, P = T['bool'] extends true ? number : string>(param: P) { }
func<typeof B>(123); //ok
func<typeof B>('random string'); //not ok
func<typeof C>(123); //not ok
func<typeof C>('random string'); //ok
function wrapper<T extends { start(...args: unknown[]): unknown }>(target: { new(): T }, ...args: Parameters<T['start']>): T {
let instance = new target();
instance.start(...args);
return instance;
}
class RandomClass {
start(arg1: string, arg2: number) { /* ... */ }
}
let instance = wrapper(RandomClass, 'Hello, world!', 777); /// все ок
let instance2 = wrapper(RandomClass, 777); /// ошибка
// A.js
class A {
constructor(some_var) {
this.some_var = some_var;
}
do() {
console.log(this.some_var);
}
}
module.exports = A;
// index.js
const A = require('./A');
function print_it(param) {
const a = new A(param);
a.do();
}
print_it('wow'); // В консоли должно вывестись: wow
print_it('123'); // В консоли должно вывестись: 123
#include <stdio.h>
#include <iostream>
#include <typeinfo>
template <class T>
class A {
public:
A(){};
~A(){};
typedef T custom;
custom num = 1;
T get() {
return num;
}
void print() {
std::cout << get() << " " << typeid(num).name() << "\n";
}
};
class B : public A<float> {
public:
B() : A() { num = 2.f; };
~B(){};
};
int main() {
B b;
b.print();
return 0;
}
typedef float custom;
A::custom = int
B::custom = float
class I
{
public:
virtual void print() = 0;
};
class A : public I
{
public:
virtual void print() override
{
std::cout << num;
}
private:
int num = 1;
};
class B : public I
{
public:
virtual void print() override
{
std::cout << num;
}
private:
float num = 2.f;
};
template <typename T>
class A
{
public:
void print()
{
std::cout << val;
}
private:
T val;
};
union conv{
char (*pcarr)[4];
int* pint;
};
static conv bconv;
int main(){
bconv.pint = new int(100);
cout << *((int*)bconv.pcarr) << "\n"; //int to char (*)[4]
bconv.pcarr = (char(*)[4])new char[4]{1,2,3,4};
cout << *bconv.pint << "\n"; //char (*)[4] to int
}
union conv{
unsigned char (*pcarr)[4];
int* vint;
};
static conv bconv;
int main(){
bconv.vint = new int(100);
cout << *((int*)bconv.pcarr) << "\n"; //int to char (*)[4]
bconv.pcarr = (unsigned char(*)[4])new unsigned char[4]{255,255,255,127}; //big-endian
if(INT_MAX == *bconv.vint) cout << "bingo\n";
}