DefinitelyTyped/types/ts-nameof/ts-nameof-tests.ts
David Sherret 56f0f57acf [ts-nameof] Add nameof.interpolate (#38706)
* ts-nameof: Add nameof.interpolate

* Fix old ts vs new ts difference with string literal types.
2019-10-03 14:31:28 -07:00

58 lines
1.8 KiB
TypeScript

// tslint:disable-next-line no-namespace
namespace TestNamespace {
export interface TestType {
prop: string;
}
}
class TestClass {
prop1 = "";
prop2 = "";
}
// nameof tests
nameof(TestClass); // $ExpectType string
nameof<TestNamespace.TestType>(); // $ExpectType string
nameof<TestClass>(t => t.prop1); // $ExpectType string
// nameof.full tests
const testInstance = new TestClass();
nameof.full(testInstance.prop1); // $ExpectType string
nameof.full(testInstance.prop1, 1); // $ExpectType string
nameof.full<TestNamespace.TestType>(); // $ExpectType string
nameof.full<TestNamespace.TestType>(1); // $ExpectType string
nameof.full<TestClass>(t => t.prop1); // $ExpectType string
nameof.full<TestClass>(t => t.prop1, 1); // $ExpectType string
// nameof.toArray tests
nameof.toArray(testInstance.prop1); // $ExpectType string[]
nameof.toArray(testInstance.prop1, testInstance.prop2); // $ExpectType string[]
nameof.toArray<TestClass>(t => [t.prop1]); // $ExpectType string[]
// nameof.interpolate tests
nameof.interpolate("" as string); // $ExpectType string
// reference type test
const myObj = { test: "" };
nameof(myObj); // $ExpectType string
nameof.full(myObj); // $ExpectType string
nameof.toArray(myObj); // $ExpectType string[]
// primitive type test
const myStr = "";
nameof(myStr); // $ExpectType string
nameof.full(myStr); // $ExpectType string
nameof.toArray(myStr); // $ExpectType string[]
// null test
const nullTypedVar = null;
nameof(nullTypedVar); // $ExpectType string
nameof.full(nullTypedVar); // $ExpectType string
nameof.toArray(nullTypedVar); // $ExpectType string[]
// undefined test
const undefinedTypedVar = undefined;
nameof(undefinedTypedVar); // $ExpectType string
nameof.full(undefinedTypedVar); // $ExpectType string
nameof.toArray(undefinedTypedVar); // $ExpectType string[]