本文整理汇总了TypeScript中invariant.default函数的典型用法代码示例。如果您正苦于以下问题:TypeScript default函数的具体用法?TypeScript default怎么用?TypeScript default使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了default函数的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: cloneWithRef
export default function cloneWithRef(
element: any,
newRef: any,
): React.ReactElement<any> {
const previousRef = element.ref
invariant(
typeof previousRef !== 'string',
'Cannot connect React DnD to an element with an existing string ref. ' +
'Please convert it to use a callback ref instead, or wrap it into a <span> or <div>. ' +
'Read more: https://facebook.github.io/react/docs/more-about-refs.html#the-ref-callback-attribute',
)
if (!previousRef) {
// When there is no ref on the element, use the new ref directly
return cloneElement(element, {
ref: newRef,
})
}
return cloneElement(element, {
ref: (node: any) => {
newRef(node)
if (previousRef) {
previousRef(node)
}
},
})
}
示例2: subscribeToStateChange
public subscribeToStateChange(
listener: Listener,
options: { handlerIds: string[] | undefined } = { handlerIds: undefined },
): Unsubscribe {
const { handlerIds } = options
invariant(typeof listener === 'function', 'listener must be a function.')
invariant(
typeof handlerIds === 'undefined' || Array.isArray(handlerIds),
'handlerIds, when specified, must be an array of strings.',
)
let prevStateId = this.store.getState().stateId
const handleChange = () => {
const state = this.store.getState()
const currentStateId = state.stateId
try {
const canSkipListener =
currentStateId === prevStateId ||
(currentStateId === prevStateId + 1 &&
!areDirty(state.dirtyHandlerIds, handlerIds))
if (!canSkipListener) {
listener()
}
} finally {
prevStateId = currentStateId
}
}
return this.store.subscribe(handleChange)
}
示例3: delay
function * aiJudgeScore () {
const { board, player, ai, version } = yield select()
const scores = []
const judge = judgeScores[version]
invariant(judge, 'version error')
const chess = getCandidate(player)
for (let r = 0; r < 8; r += 1) {
for (let c = 0; c < 8; c += 1) {
if (board[r][c] === chess) {
let score = judge(board, ai, r, c)
scores.push({
row: r,
col: c,
score: score
})
}
}
}
invariant(scores.length, 'Invalid State: Candidates not place')
const { score } = head(orderBy(scores, 'score', 'desc'))
const { row, col } = sample(filter(scores, ['score', score])) // A little random
yield delay(300) // A little delay
yield put(
pushLog({
player,
pos: `(${row}, ${col})`
})
)
yield call(flipAllChess, { row, col, player })
yield put(placeChess(row, col, player))
}
示例4: verifyInvariants
function verifyInvariants(monitor: DragDropMonitor) {
invariant(monitor.isDragging(), 'Cannot call drop while not dragging.')
invariant(
!monitor.didDrop(),
'Cannot call drop twice during one drag operation.',
)
}
示例5: verifyInvariants
function verifyInvariants(
sourceIds: string[],
monitor: DragDropMonitor,
registry: HandlerRegistry,
) {
invariant(!monitor.isDragging(), 'Cannot call beginDrag while dragging.')
for (const s of sourceIds) {
invariant(registry.getSource(s), 'Expected sourceIds to be registered.')
}
}
示例6: getCurrentPosition
function getCurrentPosition(
success: GeoSuccessCallback,
error: GeoErrorCallback = () => {},
options: LocationOptions = {}
): void {
invariant(typeof success === 'function', 'Must provide a valid success callback.');
invariant(typeof options === 'object', 'options must be an object.');
_getCurrentPositionAsyncWrapper(success, error, options);
}
示例7: validateSourceContract
export function validateSourceContract(source: DragSource) {
invariant(
typeof source.canDrag === 'function',
'Expected canDrag to be a function.',
)
invariant(
typeof source.beginDrag === 'function',
'Expected beginDrag to be a function.',
)
invariant(
typeof source.endDrag === 'function',
'Expected endDrag to be a function.',
)
}
示例8: validateTargetContract
export function validateTargetContract(target: DropTarget) {
invariant(
typeof target.canDrop === 'function',
'Expected canDrop to be a function.',
)
invariant(
typeof target.hover === 'function',
'Expected hover to be a function.',
)
invariant(
typeof target.drop === 'function',
'Expected beginDrag to be a function.',
)
}
示例9: useRef
export function useDrop<
DragObject extends DragObjectWithType,
DropResult,
CollectedProps
>(
spec: DropTargetHookSpec<DragObject, DropResult, CollectedProps>,
): [CollectedProps, ConnectDropTarget] {
const specRef = useRef(spec)
invariant(spec.accept != null, 'accept must be defined')
const [monitor, connector] = useDropTargetMonitor()
useDropHandler(specRef, monitor, connector)
const result: CollectedProps = useMonitorOutput(
monitor,
specRef.current.collect || (() => ({} as CollectedProps)),
() => connector.reconnect(),
)
const connectDropTarget = useMemo(() => connector.hooks.dropTarget(), [
connector,
])
useEffect(() => {
connector.dropTargetOptions = spec.options || null
connector.reconnect()
}, [spec.options])
return [result, connectDropTarget]
}
示例10: timeoutForField
export function timeoutForField(field: GraphQLField<any, any>) {
const fieldDirectives = field.astNode && field.astNode.directives
const directive = fieldDirectives && fieldDirectives.find(directive => directive.name.value === "timeout")
if (directive) {
const args = directive && directive.arguments
const arg = args && args[0]
invariant(arg && arg.name.value === "ms", "graphqlTimeoutMiddleware: The `@timeout(ms: âŚ)` argument is required.")
const value = arg!.value
if (value.kind === "IntValue") {
return parseInt(value.value)
} else {
invariant(false, `graphqlTimeoutMiddleware: Expected \`@timeout(ms: âŚ)\` to be a \`IntValue\`, got \`${value.kind}\` instead.`)
return null
}
}
return null
}