Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add example of pure ownPropertyNames typing #94

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions tests/iterable/ownPropertyNames.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,31 @@ describe('ownPropertyNames', () => {
const result = Array.from(_.ownPropertyNames(new TestClass()))
expect(result).toEqual(['a'])
})
test("don't provide unexpected properties after type narrowing", () => {
type FullLog = 'debug' | 'info' | 'error' | 'warn'
type ProdLog = Extract<FullLog, 'warn' | 'error'>

const error = vi.fn()
const warn = vi.fn()

const handlers = { error, warn }

function handleLogs(logs: { [key in ProdLog]: number }) {
for (const log of _.ownPropertyNames(logs)) {
handlers[log]()
}
}

const logs: { [key in FullLog]: number } = {
error: 1,
warn: 2,
info: 3,
debug: 4,
}

handleLogs(logs)

expect(error).toHaveBeenCalledTimes(1)
expect(warn).toHaveBeenCalledTimes(1)
})
})