如何写入某种类型的数据但将其导出为 const
How to write data with some type but export it as const
我现在想写一些实现某种类型的对象TestType
但将其导出为只读 const
type TestType = { someProperty: { childProperty: string } }
const obj1: TestType = { someProperty: { childProperty: 'my custom text' } } // will works
const take1: typeof obj1['someProperty']['childProperty'] = '' // will except string
const obj2 = { someProperty: { childProperty: 'my custom text' } } as const
const take2: typeof obj2['someProperty']['childProperty'] = 'my custom text' // will except 'my custom text'
我想要他们两个
该对象将实现该类型,但将导出为 const
const obj3 = ({ someProperty: { childProperty: 'my custom text' } } as TestType) as const // will casue type error
const take3: typeof obj2['someProperty']['childProperty'] = 'my custom text' // will except 'my custom text'
您可以使用已经受其他类型约束的恒等函数来实现。
您仍然需要对输入参数使用 as const
断言。这不是很符合人体工程学,但可以解决您的问题:
function createConstrainedIdFn <Constraint extends Record<PropertyKey, unknown>>(): <T extends Constraint>(obj: T) => T {
return obj => obj;
}
type TestType = { someProperty: { childProperty: string } };
const createTestType = createConstrainedIdFn<TestType>();
const obj1 = createTestType({ someProperty: { childProperty: 'my custom text' } } as const);
const take1: typeof obj1['someProperty']['childProperty'] = 'another string' /*
^^^^^
Type '"another string"' is not assignable to type '"my custom text"'.(2322) */
您可以通过使用较短的函数名称并立即调用返回的函数来使语法更简洁:
function id <Constraint extends Record<PropertyKey, unknown>>(): <T extends Constraint>(obj: T) => T {
return obj => obj;
}
type TestType = { someProperty: { childProperty: string } };
const obj1 = id<TestType>()({ someProperty: { childProperty: 'my custom text' } } as const);
我现在想写一些实现某种类型的对象TestType
但将其导出为只读 const
type TestType = { someProperty: { childProperty: string } }
const obj1: TestType = { someProperty: { childProperty: 'my custom text' } } // will works
const take1: typeof obj1['someProperty']['childProperty'] = '' // will except string
const obj2 = { someProperty: { childProperty: 'my custom text' } } as const
const take2: typeof obj2['someProperty']['childProperty'] = 'my custom text' // will except 'my custom text'
我想要他们两个 该对象将实现该类型,但将导出为 const
const obj3 = ({ someProperty: { childProperty: 'my custom text' } } as TestType) as const // will casue type error
const take3: typeof obj2['someProperty']['childProperty'] = 'my custom text' // will except 'my custom text'
您可以使用已经受其他类型约束的恒等函数来实现。
您仍然需要对输入参数使用 as const
断言。这不是很符合人体工程学,但可以解决您的问题:
function createConstrainedIdFn <Constraint extends Record<PropertyKey, unknown>>(): <T extends Constraint>(obj: T) => T {
return obj => obj;
}
type TestType = { someProperty: { childProperty: string } };
const createTestType = createConstrainedIdFn<TestType>();
const obj1 = createTestType({ someProperty: { childProperty: 'my custom text' } } as const);
const take1: typeof obj1['someProperty']['childProperty'] = 'another string' /*
^^^^^
Type '"another string"' is not assignable to type '"my custom text"'.(2322) */
您可以通过使用较短的函数名称并立即调用返回的函数来使语法更简洁:
function id <Constraint extends Record<PropertyKey, unknown>>(): <T extends Constraint>(obj: T) => T {
return obj => obj;
}
type TestType = { someProperty: { childProperty: string } };
const obj1 = id<TestType>()({ someProperty: { childProperty: 'my custom text' } } as const);